greater<int>()和less<int>()的使用
2017-08-04 10:02
3295 查看
greater和less
greater和less是头文件<xfunctional>中定义的两个结构。下面看它们 的定义,greater和less都重载了操作符()。[cpp] view
plain copy
// TEMPLATE STRUCT greater
emplate<class _Ty>
struct greater
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};
// TEMPLATE STRUCT less
emplate<class _Ty>
struct less
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return (_Left < _Right);
}
};
在sort()函数中使用greater<int>()和less<int>(),
[cpp] view
plain copy
#include<iostream>
#include<algorithm>//因为用了sort()函数
#include<functional>//因为用了greater<int>()
using namespace std;
void main()
{
int a[]={3,1,4,2,5};
int i;
int len=sizeof(a)/sizeof(int);//这里切记要除以sizeof(int)!
sort(a ,a + len, greater<int>());//内置类型的由大到小排序
for(i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<"\n";
sort(a, a + len, less<int>()); //内置类型的由小到大排序
for(i=0;i<len;i++)
cout<<a[i]<<" ";
}
相关文章推荐
- greater<int>()和less<int>()的使用
- greater<int>()和less<int>()的使用
- greater<int>()和less<int>()的使用
- POJ 3253 Fence Repair (priority_queue的用法) ----- less<int> 和 greater<int>
- C++ greater<int>()和less<int>()
- greater<int>()表示内置类型从大到小排序,less<int>()表示内置类型从小到大排序
- 关于函数适配器less< Type>以及bind1st和bind2nd的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- set<int,greater<int>>
- priority_queue<int,vector<int>,greater<int>>优先队列,按照从小到大
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- leetcode+蛇形数组,就是vector<vector<int>> 的使用
- queue vector less <int> 从大到小排列
- STL greater<T>() 和less<T>()
- poj 3253 Fence Repair greater<int>
- 不允许使用“{...}”对”std::vector<int,std::allocator<int> >“类型的对象进行初始化
- 关于vector的vector<vector<int>>的使用问题
- vector<vector<int> >的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- greater和less是头文件<functional>