Detailed description of not1, not2, bind1st and bind2nd, not1bind2nd

Source: Internet
Author: User

Detailed description of not1, not2, bind1st and bind2nd, not1bind2nd
1. Introduction

  Bind1stAndBind2ndFunction is usedBinary Function object(Binary functor, bf)Unary function object(Unary functor, uf ). For this purpose, they need two parameters:Bf to be convertedAndOne Value(V ).

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, indicating that the expression in your program should be compared with the k value. The above two expressions correspond to bind2nd. A simple understanding is to use k as the second parameter of the comparison expression. If bind1st is used, the corresponding 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 discuss how to implement the = comparison later. Let's take two examples to see the usage of bind1st and bind2nd.

  • F = std: bind1st (functor, v); 'f (x) 'is equivalent to 'functor (v, x )'
  • F = std: bind2nd (functor, v); 'f (x) 'is equivalent to 'functor (x, v )'
2. bind1st and bind2nd examples
Int a [] = {1, 2,100,200}; std: vector <int> arr (a, a + 4); // remove all elements earlier 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. 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. However, to delete elements greater than 100, you can also use bind2nd

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

The comparison mentioned above =, for example, how to implement x <= k, std also provides a good thing, not1, we can say! (X> k) and x <= k are equivalent. Let's look at the following expression:

// Remove all elements less than or equal to 100. erase (std: remove_if (arr. begin (), arr. end (), std: not1 (std: bind2nd (std: greater <int> (), 100), arr. end ());
3. not1 and not2 examples

Not1Is to constructOpposite result of PredicateOfUnary function object,Not2Is to constructOpposite result of PredicateOfBinary Function object.

3.1 not1 example 1 // not1 example 2 # include <iostream> // std: cout 3 # include <functional> // std :: not1 4 # include <algorithm> // std: count_if 5 6 struct IsOdd {7 bool operator () (const int & x) const {return x % 2 = 1 ;} 8 typedef int argument_type; 9}; 10 11 int main () {12 int values [] = {1, 2, 3, 4, 5}; 13 int cx = std: count_if (values, values + 5, std: not1 (IsOdd (); 14 std: cout <"There are" <cx <"elements with even values. \ n "; 15 return 0; 16}View Code3.2 not2 example 1 // not2 example 2 # include <iostream> // std: cout 3 # include <functional> // std: not2, std :: pai_to 4 # include <algorithm> // std: mismatch 5 # include <utility> // std: pair 6 7 int main () {8 int foo [] = {10, 20, 30, 40, 50}; 9 int bar [] = {0, 15, 30, 45, 60}; 10 std: pair <int *, int *> firstmatch, firstmismatch; 11 firstmismatch = std: mismatch (foo, foo + 5, bar, std: pair _to <int> (); 12 firstmatch = std :: mismatch (foo, foo + 5, bar, std: not2 (std: pair _to <int> (); 13 std :: cout <"First mismatch in bar is" <* firstmismatch. second <'\ n'; 14 std: cout <"First match in bar is" <* firstmatch. second <'\ n'; 15 return 0; 16}View Code

The example must containHeader file

# Include <vector>

# Include <algorithm>

# Include <functional>

Related Article

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.