zoukankan      html  css  js  c++  java
  • not1,not2,bind1st和bind2nd详解

    1.引言 

      bind1stbind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf)。为了达到这个目的,它们需要两个参数:要转换的bf一个值(v)。

         可能这么解释以后大家还不是很清楚,那么就说点白话吧。我们在做比较的时候所写的表达式像 x > k ,x < k,这里的k是一个参数表示你程序里面的表达式要和k值去比较。上面这两个表达式对应的应该是bind2nd ,简单的理解就是把k作为比较表达式的第二个参数。如果使用bind1st则对应的表达式是 k > x,k < x,也就是把k作为比较表达式的第一个参数。大家可能会注意到这里面没有=的比较,先别着急,后面将会说道如何实现=的比较。先举两个例子看看bind1st和bind2nd的用法。 

    • f = std::bind1st( functor, v)'f( x)'等价于'functor( v, x)'
    • f = std::bind2nd( functor, v); 'f( x)'等价于'functor( x, v)'

    2.bind1st和bind2nd例子

    int a[] = {1, 2, 100, 200};
    
    std::vector< int> arr(a, a + 4);
    
    // 移除所有小于100的元素
    arr.erase( std::remove_if( arr.begin(),  arr.end(),std::bind2nd( std::less< int>(), 100)), arr.end());

    这里的比较表达式相当于arr.value < 100,如果用bind1st则表达的意思就恰恰相反

    // 移除所有大于100的元素
    arr.erase( std::remove_if( arr.begin(),  arr.end(),std::bind1st( std::less< int>(), 100)), arr.end());

    这里的表达式相当于100 < arr.value,然为了实现删除大于100的元素你同样可以使用bind2nd

    // 移除所有大于100的元素
    arr.erase( std::remove_if( arr.begin(),  arr.end(),std::bind2nd( std::greater< int>(), 100)), arr.end());

    前面说道=的比较,比如说x <= k怎么实现呢,std又提供了一个好东西not1,我们可以说 !(x > k) 和 x <= k是等价的,那么我们看看下面的表达式:

    // 移除所有小于等于100的元素
    arr.erase( std::remove_if( arr.begin(),  arr.end(),std::not1(std::bind2nd( std::greater< int>(), 100))), arr.end());

    3.not1和not2例子

    not1是构造一个与谓词结果相反一元函数对象not2是构造一个与谓词结果相反二元函数对象

    3.1 not1例子

     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.
    ";
    15   return 0;
    16 }
    View Code

    3.2 not2例子

     1 // not2 example
     2 #include <iostream>     // std::cout
     3 #include <functional>   // std::not2, std::equal_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::equal_to<int>());
    12   firstmatch = std::mismatch (foo, foo+5, bar, std::not2(std::equal_to<int>()));
    13   std::cout << "First mismatch in bar is " << *firstmismatch.second << '
    ';
    14   std::cout << "First match in bar is " << *firstmatch.second << '
    ';
    15   return 0;
    16 }
    View Code

    例子需要包含头文件

    #include <vector>

    #include <algorithm>

    #include <functional>

  • 相关阅读:
    Kali之Metasploit生成apk后门控制安卓
    迅雷后台上传?干掉迅雷后台进程和服务的一个批处理
    不用第三方软件–一键开关笔记本电脑wifi热点的批处理
    【PHP】创蓝253云通讯国际短信余额查询请求demo
    【PHP】创蓝253云通讯平台国际短信API接口demo
    创蓝253云通讯平台---短信验证码接口说明
    C++调取国际短信验证码----创蓝253云通讯平台---demo
    手机空号、停机、注销,空号检测为你去除无效号码
    上市公司都被撸垮,羊毛党就真的没有办法解决了吗?
    如何用Ruby调取创蓝253短信验证码
  • 原文地址:https://www.cnblogs.com/blueoverflow/p/4737122.html
Copyright © 2011-2022 走看看