示例:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//内建函数对象 关系仿函数
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(50);
v.push_back(20);
v.push_back(40);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//sort(v.begin(), v.end(), MyCompare());
//使用内建函数对象 大于 关系仿函数
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test02()
{
//其他下例示范一种,感兴趣的可以验证一下对不对
equal_to<int>e1; //等于
not_equal_to<int>e2; //不等于
greater<int>g1; //大于
greater_equal<int>g2; //大于等于
less<int>l1; //小于
less_equal<int>l2; //小于等于
greater_equal<int>g;
bool xx = g(3, 3);
if (xx == true)
{
cout << "3大于等于3" << endl;
}
else
{
cout << "3小于等于3" << endl;
}
}
int main()
{
//test01();
test02();
return 0;
}
总结:关系仿函数中中最常用的就是 greater<>大于;