C++关系仿函数

关系仿函数

功能描述:

实现关系对比
仿函数原型:

template<class T> bool equal_to<T>       //等于
template<class T> bool not_equal<T>             //不等于
template<class T> bool greater<T>        //大于
template<class T> bool greater_equal<T>  //大于等于
template<class T> bool less<T>                  //小于
template<class T> bool less_equal<T>     //小于等于

代码示例:

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
//内建函数对象 关系仿函数
//大于 greater
class MyCompare
{
   
public:
       bool operator()(int v1, int v2)
       {
   
              return v1 > v2;
       }
};
void test01()
{
   
       vector<int>v;
       v.push_back(10);
       v.push_back(30);
       v.push_back(40);
       v.push_back(20);
       v.push_back(50);
       for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
       {
   
              cout << *it << " ";
       }
       cout << endl;
       //降序
       //sort(v.begin(), v.end(), MyCompare());
       //greater<int>()内建函数对象
       sort(v.begin(), v.end(), greater<int>());
       for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
       {
   
              cout << *it << " ";
       }
       cout << endl;
}
int main()
{
   
       test01();
       system("pause");
       return 0;
}

总结:关系仿函数中最常用的就是greater<>大于

C/C++基础 文章被收录于专栏

本专栏收录C/C++编程语言相关知识

全部评论

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务