In STL bind1st and bind2nd and not1 adapter using the above summary part copy from: http://blog.csdn.net/yzm365487848/article/details/5568608, to this blog friends thank you. When we use STLAlgorithmFor example, find_if. If the function has two parameters but the algorithm needs a one-dimensional function, we can use an adapter. For example: bind1st and bind2nd are used to adapt the function to an unary operator.
Bind1st indicates that we bind the first parameter, and bind2st indicates that we bind the second parameter. Not1 indicates the opposite of what you want.
We may not be very clear about this explanation.
The expression we wrote during the comparison is like x> K, x <K. Here
K is a parameter that indicates youProgramThe expression must be compared with the K value.
The above two expressions correspond to bind2nd.
K is the second parameter of the comparison expression. If bind1st is used, the corresponding
The expression is k> X, k <X, that is, K is used as the first parameter of the comparison expression.
You may notice that there is no = comparison here. Don't worry,
We will explain how to implement the = comparison later. Let's take two examples to see the usage of bind1st and bind2nd.
Int A [] = {1, 2,100,200 };
STD: vector <int> Arr (A, A + 4 );
// Remove all elements smaller than 100
Arr. Erase (STD: remove_if (ARR. Begin (), arr. End (),
STD: bind2nd (STD: less <int> (), 100), arr. End ());
The comparison expression is equivalent to ARR. value <100
If bind1st is used, the expression is the opposite.
// Remove all elements greater than 100
Arr. Erase (STD: remove_if (ARR. Begin (), arr. End (),
STD: bind1st (STD: less <int> (), 100), arr. End ());
The expression here is equivalent to 100 <arr. Value
You can also use bind2nd to delete elements greater than 100.
// Remove all elements greater than 100
Arr. Erase (STD: remove_if (ARR. Begin (), arr. End (),
STD: bind2nd (STD: greater <int> (), 100), arr. End ());
For example, how to implement x <= K? STD provides not1,
We can say that! (X> K) and X <= K are equivalent. Let's look at the following expression:
// Remove all elements smaller than or equal to 100
Arr. Erase (STD: remove_if (ARR. Begin (), arr. End (),
STD: not1 (STD :: bind2nd (STD: greater <int> (), 100), arr. End ());
Note: not1 indicates that the negative return value is a single objective function, while STD also has not2. It indicates that the negative return value is a dual objective function.