not1,not2,bind1st and bind2nd detailed

Source: Internet
Author: User

1. Introduction

The bind1st and bind2nd functions are used to convert a two-dollar operator (binary FUNCTOR,BF) into a unary operator (unary Functor,uf). To achieve this, they require two parameters: the BF to be converted and a value (v).

Perhaps this explanation after the people are not very clear, then speak a little vernacular it. The expressions we write when we do comparisons are like x > K, x < K, where k is a parameter indicating that the expression in your program is to be compared with the K value. The above two expressions correspond to the bind2nd, the simple understanding is that the K as the second parameter of the comparison expression. If you use bind1st, the corresponding expression is k > X,k < X, that is, K is the first parameter of a comparison expression. You may notice that there is no comparison in this, but don't worry about it, you will see how to achieve the comparison. 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[] = {12};std::vectorint4) ; // remove all elements less than 100 arr.erase (std::remove_if (Arr.begin (),  int)), Arr.end ());

The comparison expression here is equivalent to Arr.value < 100, and if you use bind1st it means the opposite.

// remove all elements greater than 100 arr.erase (std::remove_if (Arr.begin (),  int)), Arr.end ());

The expression here is equivalent to < Arr.value, but in order to delete elements greater than 100 you can also use bind2nd

// remove all elements greater than 100 arr.erase (std::remove_if (Arr.begin (),  int)), Arr.end ());

Before said = 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, let's look at the following expression:

// remove all elements less than or equal to 100 arr.erase (std::remove_if (Arr.begin (),  int)), Arr.end ());
3. Description

not1 is a negation of the return value is a single-purpose function , STD also has Not2 it is negative return value is the function of the binocular

3.1 not1 Example
1 //NOT1 Example2#include <iostream>//Std::cout3#include <functional>//std::not14#include <algorithm>//std::count_if5 6 structisodd {7   BOOL operator() (Const int& x)Const{returnX%2==1;}8typedefintArgument_type;9 };Ten  One intMain () { A   intValues[] = {1,2,3,4,5}; -   intCX = std::count_if (values, values+5, Std::not1 (IsOdd ())); -Std::cout <<"there is"<< CX <<"elements with even values.\n"; the   return 0; -}
View Code3.2 Not2 Example
1 //Not2 Example2#include <iostream>//Std::cout3#include <functional>//Std::not2, Std::equal_to4#include <algorithm>//Std::mismatch5#include <utility>//std::p air6 7 intMain () {8   intFoo[] = {Ten, -, -, +, -};9   intBar[] = {0, the, -, $, -};Tenstd::p air<int*,int*>Firstmatch,firstmismatch; OneFirstmismatch = Std::mismatch (foo, foo+5, Bar, std::equal_to<int>()); AFirstmatch = Std::mismatch (foo, foo+5, Bar, Std::not2 (std::equal_to<int>())); -Std::cout <<"First mismatch in bar are"<< *firstmismatch.second <<'\ n'; -Std::cout <<"First match in Bar is"<< *firstmatch.second <<'\ n'; the   return 0; -}
View Code

Examples need to include header files

#include <vector>

#include <algorithm>

#include <functional>

not1,not2,bind1st and bind2nd detailed

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.