The preceding functions can be used to call the for_each algorithm. However, this problem may occur, what if we need three or more parameters to call the for_each algorithm? The bind1st in STL is obviously not working, because it only supports two parameters at most. What else can be done? In this case, you need to use the powerful bind in the boost library. It not only applies more than the STL library, but also is more convenient and more humane. The following is an example of how to bind a class member function with three parameters:
[CPP]View plaincopy
- // Call a member function of the class with more than two parameters.
- // Software developer: Cai junsheng 2013-02-10
- // QQ: 9073204
- Class cobjbind
- {
- Public:
- Void test (void)
- {
- //
- STD: vector <int> vrect;
- Vrect. push_back (1 );
- Vrect. push_back (2 );
- Vrect. push_back (3 );
- Vrect. push_back (10 );
- // Use the binder to call the class member function
- STD: cout <"First:" <STD: Endl;
- STD: for_each (vrect. Begin (), vrect. End (),
- Boost: BIND (& cobjbind: Fun, this, _ 1, 1, 2 ));
- STD: cout <"Second:" <STD: Endl ;;
- STD: for_each (vrect. Begin (), vrect. End (),
- Boost: BIND (& cobjbind: Fun, this, 1, _ 1, 2 ));
- }
- PRIVATE:
- Void fun (int x, int y, int Z)
- {
- STD: cout <"cobjbind: Fun:" <x <"," <Y <"," <z <STD: Endl;
- }
- };
In this example, we can see the line of code using the binder, as shown below:
Boost: BIND (& cobjbind: Fun, this, _ 1, 1, 2)
The first parameter of the bind directly uses the class member function pointer. It is not like adding the mem_fun function for conversion. The second parameter is to continue to input the this pointer; the third parameter is _ 1, and the underlined 1 is strange. In fact, this is a placeholder of the BIND, which is used to specify the order in which the generated function object parameters are passed to the bound function parameter; the last two parameters are transmitted to the function parameters. In this way, the void fun (int x, int y, int Z) function is transmitted, and fun (_, 2) is called in this way ). The output result is as follows:
Cobjbind: Fun: 1, 1, 2
Cobjbind: Fun: 2, 1, 2
Cobjbind: Fun: 3, 1, 2
Cobjbind: Fun: 10, 1, 2
From the output result, we can see that the placeholder is where the for_each algorithm passes in the parameter. The call is as follows:
Fun (x, 1, 2) (x );
Next we can see that the placeholder is placed in the second position, as shown below:
STD: for_each (vrect. Begin (), vrect. End (),
Boost: BIND (& cobjbind: Fun, this, 1, _ 1, 2 ));
Here, placeholder _ 1 is placed in the second parameter. The output result is as follows:
Cobjbind: Fun: 1, 1, 2
Cobjbind: Fun: 1, 2
Cobjbind: Fun: 1, 3, 2
Cobjbind: Fun: 1, 10, 2
In the preceding comparison, the value passed in by the algorithm for_each is passed in by the second parameter.
The BIND in the boost library has more parameters than the two defined in STL. You may ask, what if there are more than nine bind parameters? There are only nine or more cases. Even if there is one, you need to define a Data Structure to store the data. It is more cost-effective to pass a pointer, because there are fewer parameters, it is easy to maintain and the transmission efficiency is also improved. The second advantage is that it is simple to bind a member function and does not need to call the mem_fun function. The third advantage is that a placeholder is provided, which can be specified as a parameter. The placeholder names of the nine parameters are _ 1, _ 2, _ 3, and are always attached to _ 9.
The bindings in the boost library are compatible with each other and support bind of binary computing functions in STL as follows:
Bool bresult = BIND (STD: less <int> (), 5, 9 )();
STD: cout <bresult <STD: Endl;
Next let's look at a complicated example, such as making such a judgment on the value x:
If (x> 10 & x <= 20)
{
}
You can use the binder as follows:
// Implement if (x> 10 & x <= 20)
Boost: BIND (STD: logical_and <bool> (),
Boost: BIND (STD: greater <int> (), _ 1, 10 ),
Boost: BIND (STD: less_equal <int> (), _ 1, 20) (15 );
Through such a combination, we can implement complex expression operations, so that we can provide more complex conditions for the algorithm and achieve what we need to achieve.
In the following example, the elements of a specified condition can be deleted:
Vrect. Erase (STD: remove_if (vrect. Begin (), vrect. End (),
Boost: BIND (& cobjbind: isless, this, _ 1), vrect. End ());
In this example, the remove_if algorithm is called to delete elements smaller than the specified conditions in the array, return the starting position of the invalid elements, and call vrect. erase deletes invalid elements, and then leaves valid elements in the array.
In short, the binder has become an indispensable part of the boost library. For example, libraries such as algorithms, threads, and networks all need to be adapted using the binder, it is efficient, concise, and convenient.
Boost library in work (15) bind the device and function object 3