例子需要包含头文件

#include <vector>

#include <algorithm>

#include <functional>

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)'
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>(), ))), arr.end());

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

// not1 example
#include <iostream> // std::cout
#include <functional> // std::not1
#include <algorithm> // std::count_if struct IsOdd {
bool operator() (const int& x) const {return x%==;}
typedef int argument_type;
}; int main () {
int values[] = {,,,,};
int cx = std::count_if (values, values+, std::not1(IsOdd()));
std::cout << "There are " << cx << " elements with even values.\n";
return ;
}
 // not2 example
#include <iostream> // std::cout
#include <functional> // std::not2, std::equal_to
#include <algorithm> // std::mismatch
#include <utility> // std::pair int main () {
int foo[] = {,,,,};
int bar[] = {,,,,};
std::pair<int*,int*> firstmatch,firstmismatch;
firstmismatch = std::mismatch (foo, foo+, bar, std::equal_to<int>());
firstmatch = std::mismatch (foo, foo+, bar, std::not2(std::equal_to<int>()));
std::cout << "First mismatch in bar is " << *firstmismatch.second << '\n';
std::cout << "First match in bar is " << *firstmatch.second << '\n';
return ;
}

not1,not2,bind1st,bind2nd的更多相关文章

  1. not1,not2,bind1st和bind2nd详解

    1.引言 bind1st和bind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf).为了达到这个目的,它们需要两个参数:要转 ...

  2. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  3. 【转】 bind1st bind2nd的使用

    以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法. bind1st和bind2nd函 ...

  4. 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd

    http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...

  5. bind1st bind2nd的使用

    STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...

  6. Effective STL

    第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...

  7. 《Effective STL》学习笔记

    http://www.cnblogs.com/arthurliu/archive/2011/08/07/2108386.html 作者:咆哮的马甲 出处:http://www.cnblogs.com/ ...

  8. stl的仿函数adapter

    Stl的一点思考 编程语言是为编译器写一份策略,如果将这份策略模板化那就是泛型编程了 bind1st bind2nd not1 not2 adapter并不改变仿函数接口,只是将参数引入其他的运算流程

  9. boost::bind 学习

    最近学习了太多与MacOS与Iphone相关的东西,因为不会有太多人有兴趣,学习的平台又是MacOS,不太喜欢MacOS下的输入法,所以写下来的东西少了很多.    等我学习的东西慢慢的与平台无关的时 ...

随机推荐

  1. if switch for while

    ---恢复内容开始--- 一.if 说明:判断表达式,看结果执行语句体 public class IfDemo2 {             public static void main(Strin ...

  2. springboot+mybatisplus 测试代码生成

    测试springboot + mybatisplus 实现代码生成   使用默认的模板引擎 pom.xml文件 <?xml version="1.0" encoding=&q ...

  3. 关于Vue-cli 跨域,即使是非自己的服务器也可以get到内容

    刚入门vue ,打算用vue的脚手架做一个小项目.需要用到第三方的api,无奈遇到各种各样的问题. 比如 Access-Control-Allow-Origin ,或者使用了ajax的jsonp模式之 ...

  4. struts2入门第一天----------一个简单例

    搭建完环境后就可以动手去打代码了.首先创建一个简单的提交表单的jsp页面(html页面也可以), <%@ page language="java" import=" ...

  5. phpstudy配置域名后apache无法启动

    1.设置域名后重启 apache停止了 检查步骤1.php路径不要有中文,phpstudy重新安装在无中文路径 2.检查80端口是否被占用,如果被占用可以停止该程序或者修改apache/nginx 端 ...

  6. php 使用当前时间点进行时间范围查询

    /** * 判断是否是吃早饭时间 */ $nowtime = time(); $start = strtotime('8:30:00'); $end = strtotime('9:30:00'); i ...

  7. mysql日志管理#二进制日志详解

    查看MySQL二进制文件中的内容有两种方式 mysqlbinlog SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row ...

  8. django开发傻瓜教程-3-celery异步处理

    Ref: https://www.jianshu.com/p/6f8576a37a3e https://blog.csdn.net/Demo_3/article/details/78119951 ht ...

  9. 登録更新(BAPI)

    購買管理(MM) * [BAPI_REQUISITION_CREATE] 購買依頼登録 * [BAPI_REQUISITION_CHANGE] 購買依頼変更 * [BAPI_REQUISITION_D ...

  10. python2.7入门---网络编程(socket)

        Python 提供了两个级别访问的网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别 ...