#include <iostream>
#include <algorithm>
using namespace std;
#include <string>
#include <vector>
#include <functional>
//一元谓词
class GreaterFive {
public:
bool operator()(int val) {
return val > 5;
}
};
//二元谓词
class MyCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test601() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end())
cout << "no find" << endl;
else
cout << "find this num:" << *it << endl;
sort(v.begin(), v.end());
//使用函数对象 改变算法策略
sort(v.begin(), v.end(),MyCompare());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test602() {
//一元仿函数 struct / class T negate
negate<int>n;
cout << n(50) << endl;
cout << negate<double>()(50.2) << endl;
//er yuan
plus<int> p;
cout << p(24, 36) << endl;
cout << greater<double>()(53, 33) << endl;
}
void printInt(vector<int> & v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test603() {
vector<int> v;
v.push_back(4);
v.push_back(3);
v.push_back(1);
v.push_back(46);
sort(v.begin(), v.end(), greater<int>());
printInt(v);
sort(v.begin(), v.end(), less<>());
printInt(v);
sort(v.begin(), v.end(), greater<>{});
printInt(v);
}
int main() {
//test601();
//test602();
test603();
return 0;
}