zoukankan      html  css  js  c++  java
  • C++11 bind和function用法

    C++11 bind和function用法

    function是一个template,定义于头文件functional中。通过function<int(int, int)>声明一个function类型,它是“接收两个int类型,返回一个int类型”的可调用对象(int (*p)(int, int))。

    function的用法:

    普通函数示例:

    #include <iostream>
    #include<functional>
    using namespace std;
    void printA(int a)
    {
        cout << "值:" << a << endl;
    }
    int main()
    {
        function<void(int)> func = printA;
        func(2);
        getchar();
    }
    

    保存lambda表达式:

    int main()
    {
        function<void(int)> func = [](int a) { cout << "值2:" << a << endl; };
        func(2);
        getchar();
    }
    

    成员函数:

    #include <iostream>
    #include<functional>
    using namespace std;
    class Foo
    {
    public:
        int num;
        Foo(int num)
            :num(num) {}
        void printA(int i) const
        {
            cout << this->num + i << endl;
        }
    };
    int main()
    {
        function<void(const Foo&, int)> f = &Foo::printA;
        Foo foo(2);
        f(foo, 5);
        getchar();
    }
    

    bind的用法

    可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。

    调用bind的一般形式:auto newCallable = bind(callable, arg_list);

    其中,newCallable本身是一个可调用对象,arg_list是一个逗号分割的参数列表,对应给定的callable的参数。

    示例:

    #include <iostream>
    #include<functional>
    using namespace std;
    class A
    {
    public:
        void fun_3(int k, int m)
        {
            cout << "print: k=" << k << ", m=" << m << endl;
        }
    };
    void fun_1(int x, int y, int z)
    {
        cout << "print: x = " << x << ", y=" << y << ",z=" << z << endl;
    }
    void fun_2(int& a, int& b)
    {
        a++;
        b++;
        cout << "print: a=" << a << ",b=" << b << endl;
    }
    int main()
    {
        auto f1 = ::bind(fun_1, 2, 3, 5);
        f1();
    
        auto f2 = ::bind(fun_1, placeholders::_1, placeholders::_2, 3);
        //表示绑定函数fun的第三个参数为3,而fun的第一、二个参数分别由调用f2的第一、二个参数指定
        f2(3, 34);
        auto f3 = ::bind(fun_1, placeholders::_2, placeholders::_1, 3);
        //表示绑定函数fun的第三个参数为3,而fun的第一、二个参数分别由调用f3的第二、一个参数指定。
        f3(1, 2);
    
        int m = 2, n = 3;
        auto f4 = ::bind(fun_2, placeholders::_1, n); 
        f4(m);
        cout << "m=" << m << endl; //m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的
        cout << "n=" << n << endl;// n=3 说明:bind对于预先绑定的函数参数是通过值传递的
    
        A a;
        auto f5 = ::bind(&A::fun_3, a, placeholders::_1, placeholders::_2);
        f5(10, 20);//调用a.fun_3(10, 20)
    
        std::function<void(int, int)> fc = ::bind(&A::fun_3, a, placeholders::_1, placeholders::_2);
        fc(10, 220);
        getchar();
    }
    
  • 相关阅读:
    Python运算符,基本数据类型
    Python2 错误记录1File "<string>", line 1, in <module> NameError: name 'f' is not defined
    用户登录三次练习
    跟我一起学Python-day1(条件语句以及初识变量)
    vim operation
    步步为营-28-事件本质
    步步为营-27-事件
    步步为营-26-多播委托
    步步为营-25-委托(比大小)
    步步为营-24-委托
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/14976645.html
Copyright © 2011-2022 走看看