c++ function bind placeholders

#include <iostream>  
#include <functional>  
using namespace std;  
class A  
{  
public:  
    void fun_3(int k,int m)  
    {  
        cout<<k<<" "<<m<<endl;  
    }  
};  
  
void fun(int x,int y,int z)  
{  
    cout<<x<<"  "<<y<<"  "<<z<<endl;  
}  
  
void fun_2(int &a,int &b)  
{  
    a++;  
    b++;  
    cout<<a<<"  "<<b<<endl;  
}  
  
int main(int argc, const char * argv[])  
{  
    //f1的类型为 function<void(int, int, int)>  
    auto f1 = std::bind(fun,1,2,3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3  
    f1(); //print:1  2  3  
  
    auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3);  
    //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定  
    f2(1,2);//print:1  2  3  
  
    auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3);  
    //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定  
    //注意: f2  和  f3 的区别。  
    f3(1,2);//print:2  1  3  
  
  
    int n = 2;  
    int m = 3;  
  
    auto f4 = std::bind(fun_2, n,placeholders::_1); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。  
    f4(m); //print:3  4  
  
    cout<<m<<endl;//print:4  说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m  
    cout<<n<<endl;//print:2  说明:bind对于预先绑定的函数参数是通过值传递的,如n  
  
  
    A a;  
    //f5的类型为 function<void(int, int)>  
    auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2); //使用auto关键字  
    f5(10,20);//调用a.fun_3(10,20),print:10 20  
  
    std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);  
    fc(10,20);//调用a.fun_3(10,20) print:10 20  
  
    return 0;  
}  

==========

通过map下标即可调用各种可调用对象,完成各种运算

map<string,function<int(*)(int,int)>> binpos; 
map<string,function<int(*)(int,int)>> binpos= 

    {"+",add}, {"-",std::minus<int>()}, //标准库函数对象 
    {"/",divide()}, //用户定义的函数对象 {"%",mod}, //命名的lambda表达式 
    {"*",[](int a,int b){return a*b;}}, //未命名的lambda表达式 
};

binops["+"](1,2); //相当于调用add(1,2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值