greater<int>()和less<int>()的使用
2013-05-05 12:00
531 查看
greater和less
greater和less是头文件<xfunctional>中定义的两个结构。下面看它们 的定义,greater和less都重载了操作符()。// TEMPLATE STRUCT greater template<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 template<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>(),
#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>()的使用
- 关于函数适配器less< Type>以及bind1st和bind2nd的使用
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序
- greater<int>()和less<int>()的使用
- map<int,int>函数的简单使用
- greater<int>()和less<int>()的使用
- 题目1554:区间问题 map<int , vector<int> >的使用
- greater<int>()的用法
- 使用GDB调试GO<一>
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- autocomplete 属性在html <input autocomplete="on|off">中的使用
- 使用<br>标签分行显示文本
- MVC 下 JsonResult 的使用方法(JsonRequestBehavior.AllowGet)<转>
- 记住:不要使用!=,~=,^=,<>,=与NULL做比较
- Chuck语言学习笔记——3.int&float以及<<<>>>更高级的用法
- Entry<>使用
- struct2笔记之<s:set />的使用
- 可替换string的使用 & <xliff:g>标签介绍
- vs2005 使用Speech SDK包含<sphelper.h>报错的问题