Use of adapters such as bind1st and bind2nd and NOT1 in STL

Source: Internet
Author: User
use of adapters such as bind1st and bind2nd and NOT1 in STLThe above summary part copies from: http://blog.csdn.net/yzm365487848/article/details/5568608, thanked the blog friend.
When we use some of the STL algorithms, such as find_if, you need to use the imitation function, if the affine function has 2 parameters, but the algorithm needs a unary affine function, we can use the adapter, such as: bind1st and bind2nd to fit the functor into a unary operator.
bind1st says we bind the first argument, and bind2st says we bind the second argument. Not1 says it's the opposite of what you want.

Perhaps this explanation after everyone is not very clear, then speak a little vernacular bar.
The expressions we write in comparison are like x > K, x < K, where the
K is a parameter that indicates that the expression in your program is compared to the K value.
The above two expressions correspond to the bind2nd, the simple understanding is to
K as the second parameter of the comparison expression. If you use bind1st, the corresponding
The expression is k > X,k < x, which is the first parameter of the comparison expression.
You may notice that there is no comparison in this, don't worry,
The next is going to say how to implement = comparison. Let's take two examples to see the use of bind1st and bind2nd.



int a[] = {1, 2, 100, 200};

std::vector< int> arr (A, A + 4);




Remove all elements less than 100
Arr.erase (Std::remove_if (Arr.begin), Arr.end (),
std::bind2nd (std::less< int> ()), Arr.end ());

The comparison expression here is equivalent to Arr.value < 100





If you use bind1st, it means the opposite.

Remove all elements greater than 100
Arr.erase (Std::remove_if (Arr.begin), Arr.end (),
std::bind1st (std::less< int> ()), Arr.end ());

The expression here is equivalent to < Arr.value



Of course you can also use bind2nd to remove elements greater than 100.

Remove all elements greater than 100
Arr.erase (Std::remove_if (Arr.begin), Arr.end (),
std::bind2nd (std::greater< int> ()), Arr.end ());



In front of the comparison, for example, X <= K How to achieve it, STD also provides a good thing not1,
We can say! (x > K) and x <= K are equivalent, so let's look at the following expression:

Remove all elements that are less than or equal to 100
Arr.erase (Std::remove_if (Arr.begin), Arr.end (),
STD::NOT1 (std::bind2nd (std::greater< int> ())), Arr.end ());

Description: NOT1 is a negative return value is a single purpose function, STD and not2 It is the negative return value is the binocular function.



Contact Us

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.