44 lines
949 B
C++
44 lines
949 B
C++
|
// 取反适配器
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
#include <algorithm>
|
||
|
|
||
|
using namespace std;
|
||
|
// 自定义一个适配器,只需要容器中元素即可。
|
||
|
class GT5 : public unary_function<int, bool>
|
||
|
{
|
||
|
public:
|
||
|
bool operator()(const int &n) const
|
||
|
{
|
||
|
return n > 5;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class GTN : public binary_function<int, int, bool>
|
||
|
{
|
||
|
public:
|
||
|
bool operator()(const int &n1, const int &n2) const
|
||
|
{
|
||
|
return n1 > n2;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int m[] = {1, 2, 2, 3, 5, 10};
|
||
|
vector<int> v;
|
||
|
v.assign(m, m + sizeof(m) / sizeof(m[0]));
|
||
|
|
||
|
// find_if(start, end, _callback) 返回查找到的第一个迭代器的位置
|
||
|
vector<int>::iterator ret = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 3)));
|
||
|
cout << *ret << endl;
|
||
|
|
||
|
ret = find_if(v.begin(), v.end(), GT5());
|
||
|
cout << *ret << endl;
|
||
|
|
||
|
ret = find_if(v.begin(), v.end(), bind2nd(GTN(), 3));
|
||
|
cout << *ret << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|