C++ 函数对象适配器

bind1st,bind2nd:将二元函数对象配置为一元函数对象

bind1st 和 bind2nd 区别: bind1st是把传入参数绑定第一个参数,bind2nd 是把传入参数绑定第二个参数

 
not1 not2:取反

not1 和 not2的区别:not1是针对一元函数对象, not2是针对二元函数对象

 
ptr_fun 将普通函数转变为函数对象
 
mem_fun、mem_fun_ref 将成员函数转变为函数对象

1. bind1st,bind2nd

  • 第一步:继承binary_function<参数1,参数2,返回>
  • 第二步:void operator()(int v1, int v2) 加上const成为常函数
  • 第三步:实现函数体
  • 第四步:用bind1st 、bind2nd 绑定函数对象
// 第一步:继承binary_function<参数1,参数2,返回>
struct MyFunc2 : public binary_function<int, int, void>
{
    void operator()(int v1, int v2) const // 第二步:加上const成为常函数
    {
        cout << "v1 =  " << v1 << endl;
        cout << "v2 =  " << v2 << endl;

        cout << v1 + v2 << " " << endl; // 第三步 实现函数体
    }
};

void test02()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    // 第四步 用bind2nd 绑定函数对象
    for_each(v.begin(), v.end(), bind2nd(MyFunc2(), 100)); // MyFunc2() 匿名对象
    cout << endl;

    cout << "--------------------" << endl;
    for_each(v.begin(), v.end(), bind1st(MyFunc2(), 100)); // MyFunc2() 匿名对象
    cout << endl;

    // bind1st 和 bind2nd 区别
    // bind1st是把100 绑定第一个参数 bind2nd是把100绑定第二个参数
}

2. not1 , not2

  • 第一步:继承unary_function<参数,返回>
  • 第二步:bool operator()(int v) 加上const 成为常函数
  • 第三步:实现函数体
  • 第四步:not1、not2 适配 取反
// 第一步:继承unary_function<参数,返回>
struct MyNotFunc : public unary_function<int, bool>
{
    bool operator()(int v) const // 第二步 加上const 成为常函数
    {
        return v >= 20; // 第三步 实现函数体
    }
};

// not1 使用
void test03()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(30);
    v.push_back(40);
    v.push_back(20);

    // find_if
    /*
    for (; __first != __last; ++__first)
        if (__pred(*__first))
            break;
    return __first;
    */
    vector<int>::iterator it = find_if(v.begin(), v.end(), MyNotFunc()); // MyNotFunc() 函数对象
    if (it == v.end())
    {
        cout << "查找失败" << endl;
    }
    else
    {
        cout << "查找成功"
             << " " << *it << endl;
    }

    // 第四步:not1、not2 适配 取反
    vector<int>::iterator it1 = find_if(v.begin(), v.end(), not1(MyNotFunc()));
    if (it1 == v.end())
    {
        cout << "查找失败" << endl;
    }
    else
    {
        cout << "查找成功"
             << " " << *it1 << endl;
    }
}

// not2的使用
// less 二元函数对象
void myPrint(int val)
{
    cout << val << " ";
}
void test04()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(30);
    v.push_back(40);
    v.push_back(20);
    sort(v.begin(), v.end(), less<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    cout << "-------------------" << endl;
    sort(v.begin(), v.end(), not2(less<int>()));
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

3. 普通函数适配 ptr_fun

如何给一个普通函数使用绑定适配器(bind1st bind2nd)绑定一个参数?
(1) 将普通函数适配成函数对象
(2) 然后通过绑定器绑定参数

void myPrint2(int val1, int val2)
{
    cout << val1 + val2 << " ";
}
void test05()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(50);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    // 第二步: 把普通函数变为函数对象
    for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint2), 100));
    cout << endl;
}

4.成员函数适配 mem_fun、mem_fun_ref

mem_fun:如果存储的是对象指针,需要使用mem_fun
mem_fun_ref:如果存储的是对象,需要使用mem_fun_ref


class Maker
{
public:
    Maker(string name, int age)
    {
        this->name = name;
        this->age = age;
    }

    void myPrintMaker()
    {
        cout << "Name: " << this->name << " Age: " << this->age << endl;
    }

    bool myGreaterThan(int val2) {

        return this->age > val2;
    }

public:
    string name;
    int age;
};

void test06()
{
    vector<Maker> v;
    v.push_back(Maker("aaa", 10));
    v.push_back(Maker("bbb", 20));
    v.push_back(Maker("ccc", 30));
    // 当容器存储的是对象,用mem_fun_ref适配它的成员函数 (&取地址符)
    for_each(v.begin(), v.end(), mem_fun_ref(&Maker::myPrintMaker));

    cout << "--------------------" << endl;
    vector<Maker *> vp;
    vp.push_back(new Maker("aaa", 10));
    vp.push_back(new Maker("bbb", 20));
    vp.push_back(new Maker("ccc", 30));

    // 当容器存储的是对象指针,用mem_fun适配它的成员函数
    for_each(vp.begin(), vp.end(), mem_fun(&Maker::myPrintMaker));

    cout << "--------------------" << endl;

    int num = count_if(vp.begin(),vp.end(),bind2nd(mem_fun(&Maker::myGreaterThan),15));
    cout << "age>15" << "   " << num << endl;


    int num2 = count_if(vp.begin(),vp.end(),not1(bind2nd(mem_fun(&Maker::myGreaterThan),15)));
    cout << "age<=15" << "   " << num2 << endl;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容