C++ greater<int>()和less<int>()
2017-08-11 10:43
369 查看
1.greater和less是头文件<functional>
template <class T> struct greater {
bool operator() (const T& x, const T& y) const {return x>y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
2.在sort()函数中使用greater<>()和less<int>()
#include<iostream>
#include<vector>
#include<iterator>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
int A[]={1,4,3,7,10};
const int N=sizeof(A)/sizeof(int);
vector<int> vec(A,A+N);
ostream_iterator<int> output(cout," ");
cout<<"Vector vec contains:";
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter greater<int>():";
sort(vec.begin(),vec.end(),greater<int>());//内置类型从大到小
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter less<int>():";
sort(vec.begin(),vec.end(),less<int>()); //内置类型小大到大
copy(vec.begin(),vec.end(),output);
return 0;
}
template <class T> struct greater {
bool operator() (const T& x, const T& y) const {return x>y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
2.在sort()函数中使用greater<>()和less<int>()
#include<iostream>
#include<vector>
#include<iterator>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
int A[]={1,4,3,7,10};
const int N=sizeof(A)/sizeof(int);
vector<int> vec(A,A+N);
ostream_iterator<int> output(cout," ");
cout<<"Vector vec contains:";
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter greater<int>():";
sort(vec.begin(),vec.end(),greater<int>());//内置类型从大到小
copy(vec.begin(),vec.end(),output);
cout<<"\nAfter less<int>():";
sort(vec.begin(),vec.end(),less<int>()); //内置类型小大到大
copy(vec.begin(),vec.end(),output);
return 0;
}
相关文章推荐
- greater<int>()和less<int>()的使用
- POJ 3253 Fence Repair (priority_queue的用法) ----- less<int> 和 greater<int>
- greater<int>()表示内置类型从大到小排序,less<int>()表示内置类型从小到大排序
- greater<int>()和less<int>()的使用
- greater<int>()和less<int>()的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- priority_queue<int,vector<int>,greater<int>>优先队列,按照从小到大
- 【priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序】
- C++中vector<int>和vector<int*>的区别
- set<int,greater<int>>
- c++中vector<int>和vector<int*>的用法比较
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- C++中map<int,vector<int>>的初始化和访问方法
- poj 3253 Fence Repair greater<int>
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- C++中 vector<vector<int>>
- STL greater<T>() 和less<T>()
- C++ vector<int>::size_type的使用
- C++ : 出错解释 base operand of '->' has non-pointer type 'std::pair<int, int>'
- 关于STL中的greater<T>()和less<T>()