# Bind1st, bind2nd, not1, and other adapters in STL

Source: Internet
Author: User
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.

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.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.