BIND is a set of templates used for function binding. When binding a function, you can specify some or all parameters or adjust the order of parameters without specifying any parameters. You can use placeholders _ 1, _ 2, and _ 3 for unspecified parameters. -1 indicates the 1st parameters of the bound function, _ 2 indicates the 2nd parameters of the bound function, and so on. BIND can be bound to common functions, function objects, class member functions, and class member variables. The following is an introduction. 1. Common functions
1 Void Nine_arguments ( Int I1, Int I2, Int I3, Int I4, Int I5, Int I6, Int I7, Int I8, Int I9 ); 2 Int I1 = 1 , I2 = 2 , I3 = 3 , I4 = 4 , I5 = 5 , I6 = 6 , I7 = 7 , I8 = 8 , I9 =9 ; 3 BIND (nine_arguments, _ 9, _ 2, _ 1, _ 6, _ 3, _ 8, _ 4, _ 5, _ 7 (I1, I2, I3, I4, i5, I6, i7, i8, I9 ); 4 BIND (nine_arguments, I9, I2, i1, I6, I3, i8, _ 1, _ 2, _ 1) (i8, I9 ); 5 BIND (nine_arguments, I9, I2, i1, I6, I3, i8, I4, I5, i7 )();
2. Function objects
1 Class Cstudent 2 { 3 Public : 4 Void Operator ()( String Strname, Int Nage) 5 { 6 Cout <strname < " : " <Nage < Endl; 7 } 8 }; 9 BIND (cstudent (), " Mike " , _ 1 )( 12 );
3. member functions of the class
1 Struct Tadd 2 { 3 Int Add ( Int X, Int Y) 4 { 5 Return X + Y; 6 } 7 }; 8 Tadd; 9 Tadd * P = New Tadd (); 10 Shared_ptr <Tadd> * Q (P ); 11 BIND (Tadd: add, Tadd, 2 , 3 )(); 12 BIND (Tadd: add, P, 2 , 3 )(); 13 BIND (Tadd: add, Q,2 , 3 )();
4. class member variables
1 Void Output ( Const String & Name) 2 { 3 Cout <name < Endl; 4 } 5 6 Map < Int , String > Map1; 7 For_each (map1.begin (), map1.end (), BIND (output, BIND (Map < Int , 8 String >:: Value_type: Second, _ 1 )));
BIND can also be nested. Assume there is a cperson class, which has an interface for getting age int getage (). Now there is a vector of the cperson object, which needs to be sorted, you can use bind as follows:
1Vector <cperson>Vctperson;2Sort (vctperson. Begin (), vctperson. End (), BIND (less <Int>(),3BIND (cperson: getage, _ 1), BIND (cperson: getage, _ 2 )));
Suppose there is a vector of integers. To get the number of integers greater than 20 but less than 30, the following are:
1Count_if (vctnum. Begin (), vctnum. End, BIND (logic_and <Bool>(),2BIND (greater <Int> (), _ 1,20), BIND (less <Int> (), _ 1,30)));
When using bind, you need to pay special attention to the following points.1. For parameters with specified values, the function objects returned by BIND will save these values, and are saved by default in the value passing mode. Consider the followingCode:
1 VoidINC (Int& A) {A ++;}2 IntN =0;3BIND (Inc, n )();
After calling the function object returned by BIND, n is still equal to 0. This is because when BIND is passed in, it is a copy of N. If you need to input n references, you can use the ref or CREF function, for example:
1BIND (Inc,Ref(N ))();//N is equal to 1 now
2. The first parameter of BIND is a function object and cannot be replaced by placeholders. Consider the following code:
1Typedef function <Void(Int)>Func;2Vector <func>Vctfunc;3For_each (vctfunc. Begin (), vctfunc. End (), BIND (_ 1,5));//Compilation Error
In this case, you can use the apply template. The first parameter of the apply template is the input function object, which can be followed by several parameters, indicating the parameters of the function object. For example:
1Apply <Void>;//Void is the return value type of the function object.2A (f );//Equivalent to calling F ()3A (f, x );//It is equivalent to calling f (x)4A (f, x, y );//It is equivalent to calling f (x, y)
After applying, we can pass the elements in vctfunc as placeholders. The reference code is as follows:
1For_each (vctfunc. Begin (), vctfunc. End (), BIND (apply <Void> (), _ 1,5));