1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| ##include <iostream> ##include <functional> using namespace std; class A { public: void fun_3(int k,int m) { cout << "fun_3 a = " << a<< endl; cout<<"print: k="<<k<<",m="<<m<<endl; } void fun_3(string str) { cout<<"print: str="<<str<<endl; } int a; }; void fun_1(int x,int y,int z) { cout<<"fun_1 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() {
cout << "\n\nstd::bind(fun_1, 1, 2, 3) -----------------\n"; auto f1 = std::bind(fun_1, 1, 2, 3); f1(); cout << "\n\nstd::bind(fun_1, 10, 20, 30) -----------------\n"; auto f11 = std::bind(fun_1, 10, 20, 30); f11(); cout << "\n\nstd::bind(fun_1, placeholders::_1,placeholders::_2, 3) -----------------\n"; auto f2 = std::bind(fun_1, placeholders::_1, placeholders::_2, 3); f2(1,2); f2(10,21,30);
cout << "\n\nstd::bind(fun_1,placeholders::_2,placeholders::_1,3) -----------------\n"; auto f3 = std::bind(fun_1,placeholders::_2,placeholders::_1,3); f3(1,2); cout << "\n\nstd::bind(fun_2, placeholders::_1, n) -----------------\n"; int m = 2; int n = 3; auto f4 = std::bind(fun_2, placeholders::_1, n); f4(m); cout<<"m="<<m<<endl; cout<<"n="<<n<<endl;
cout << "\n\nstd::bind(&A::fun_3, &a,placeholders::_1,placeholders::_2) -----------------\n"; A a; a.a = 10; auto f5 = std::bind((void(A::*)(int, int))A::fun_3, &a, 40, 50); f5(10,20);
cout << "\n\nstd::bind(&A::fun_3, &a2,placeholders::_1,placeholders::_2) -----------------\n"; A a2; a2.a = 20; auto f6 = std::bind((void(A::*)(int, int))&A::fun_3, &a2,placeholders::_1,placeholders::_2); cout << "\n\nstd::bind(&A::fun_3,a,std::placeholders::_1,std::placeholders::_2) -----------------\n"; std::function<void(int,int)> fc = std::bind((void(A::*)(int,int))&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); fc(10,20); fc = std::bind((void(A::*)(int, int))&A::fun_3,a2,std::placeholders::_1,std::placeholders::_2); cout << "\n\nstd::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2) -----------------\n"; auto f_str = std::bind((void(A::*)(string))&A::fun_3,a,std::placeholders::_1); f_str("darren");
return 0; }
|