1、适配器


2、函数适配器

int a[] = {1, 2, 3, 4, 5};
vector <int> v(a, a + 5);
cout << count_if(v.begin(), v.end(), isOdd) << endl;
cout << count_if(v.begin(), v.end(), binder2nd(modulus<int>(), 2)) << endl;
cout << count_if(v.begin(), v.end(), binder1st(less<int>(), 4)) << endl;
3、针对成员函数的函数适配器

Person(const string& name) : name_(name)
void printWithPrefix(string prefix) const
cout << prefix << name_ << endl;
void foo(const vector& v) for_each(v.begin(), v.end(), mem_fun_ref(&Person::print));
for_each(v.begin(), v.end(), binder2nd(mem_fun_ref(&Person::printWithPrefix), "Person: "));
void foo2(const vector& v) for_each(v.begin(), v.end(), mem_fun(&Person::print));
for_each(v.begin(), v.end(), binder2nd(mem_fun(&Person::printWithPrefix), "Person: "));
v.push_back(Person("tom"));
v.push_back(Person("jer"));
v2.push_back(new Person("tom"));
v2.push_back(new Person("jer"));
4、针对一般函数的函数适配器
char* a[] = {"", "BBB", "CCC"};
vector<char*> v(a, a + 2);
vector<char*>::iterator it;
it = find_if(v.begin(), v.end(), binder2nd(ptr_fun(strcmp), ""));