19.2.2 二元谓词
二元谓词在之前已经接触过,在sort排序中。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
void test1()
{
vector<int>v;
v.push_back(30);
v.push_back(10);
v.push_back(20);
v.push_back(40);
v.push_back(50);
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << '\t';
}
cout << endl;
//使用函数对象修改排序规则
sort(v.begin(), v.end(), MyCompare());
for (vector<int>::iterator it = v.begin();it != v.end();it++)
{
cout << *it << '\t';
}
cout << endl;
}
int main()
{
test1();
//std::cout << "Hello World!\n";
}