1. 仿函数适配器 bind1st、bind2nd 将二元仿函数转为一元仿函数

  2. 仿函数适配器 not1、not2

  3. 仿函数适配器 ptr_fun 将普通函数转为函数对象,然后就可以与其它仿函数适配器一起使用了

  4. 仿函数适配器 mem_fun、mem_fun_ref 将成员函数转为适配器

class MyPrint : public binary_function<int, int, void>{
public:
    void operator()(int v, int val) const{
        cout << "v: " << v << ", val: " << val << ", v + val: " << v + val << endl;
    }
};
void myPrint(int v, int val){
    cout << v + val << " ";
}
class MySort: public binary_function<int, int, bool>{
public:
    bool operator() (int lhs, int rhs)const{
        return lhs > rhs;
    }
};
class MyGreater: public unary_function<int, bool>{
public:
    bool operator()(int v)const{
        return v > 50;
    }
};

void printVec(const vector<int> &v){
    for(const auto &p : v){
        cout << p << " ";
    }
    cout << endl;
}
class Person{
public:
    Person(int id, int age):id(id), age(age){}
    void show(){
        cout << "id: " << id << ", age: " << age << endl;
    }
    int id;
    int age;
};
int main(){

    vector<int> v;
    for(int i = 0; i < 10; ++i){
        v.push_back(rand() % 100);
    }
    // bind1st bind2nd
    for_each(v.begin(), v.end(), bind1st(MyPrint(), 100));
    cout << endl;
    // ptr_fun
    for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint), 100)); 
    cout << endl;
    printVec(v);
    // not1 not2
    sort(v.begin(), v.end(), not2(MySort()));
    printVec(v);
    auto it = find_if(v.begin(), v.end(), not1(MyGreater()));
    if(it != v.end()){
        cout << *it << endl;
    }
    // mem_fun_ref mem_fun
    vector<Person> vp;
    vp.emplace_back(Person(1, 2));
    vp.emplace_back(Person(3, 4));
    vp.emplace_back(Person(5, 6));
    for_each(vp.begin(), vp.end(), mem_fun_ref(&Person::show));
    cout << "--------------" << endl;
    vector<Person*> vpp;
    vpp.emplace_back(new Person(1, 2));
    vpp.emplace_back(new Person(3, 4));
    vpp.emplace_back(new Person(5, 6));
    for_each(vpp.begin(), vpp.end(), mem_fun(&Person::show));
    return 0;
}
v: 100, val: 83, v + val: 183
v: 100, val: 86, v + val: 186
v: 100, val: 77, v + val: 177
v: 100, val: 15, v + val: 115
v: 100, val: 93, v + val: 193
v: 100, val: 35, v + val: 135
v: 100, val: 86, v + val: 186
v: 100, val: 92, v + val: 192
v: 100, val: 49, v + val: 149
v: 100, val: 21, v + val: 121

183 186 177 115 193 135 186 192 149 121 
83 86 77 15 93 35 86 92 49 21 
15 21 35 49 77 83 86 86 92 93 
15
id: 1, age: 2
id: 3, age: 4
id: 5, age: 6
--------------
id: 1, age: 2
id: 3, age: 4
id: 5, age: 6