std::bind

bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
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; } 1、绑定函数fun的第一,二,三个参数值为: 1 2 3 auto f1 = std::bind(fun,1,2,3); f1();//print:1 2 3 2、绑定函数 fun 的第三个参数为3,而fun的第一,二个参数由调用f2指定 auto f2 = std::bind(fun, placeholders::_1, placeholders::_2, 3); f2(1,2);//print:1 2 3 3、绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定 auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3); f3(1,2);//print:2 1 3 4、绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。 int n = 2; int m = 3; auto f4 = std::bind(fun_2, n,placeholders::_1); //bind(fun_2, 2, 3) f4(m); //print:3 4 5、绑定类的成员函数 A a; auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2); std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); f5(10,20);//调用a.fun_3(10,20),print:10 20

猜你喜欢

转载自www.cnblogs.com/osbreak/p/10080516.html
今日推荐