STL中bind1st和bind2nd的区别
来源:互联网 发布:8月进出口数据 编辑:程序博客网 时间:2024/05/18 13:25
bind1st和bind2nd函数用于将一个二元算子(binary functor,bf)转换成一元算子(unary functor,uf)。为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v)。
可能这么解释以后大家还不是很清楚,那么就说点白话吧。我们在做比较的时候所写的表达式像 x > k ,x < k,这里的k是一个参数表示你程序里面的表达式要和k值去比较。上面这两个表达式对应的应该是bind2nd ,简单的理解就是把k作为比较表达式的第二个参数。如果使用bind1st则对应的表达式是 k > x,k < x,也就是把k作为比较表达式的第一个参数。大家可能会注意到这里面没有=的比较,先别着急,后面将会说道如何实现=的比较。先举两个例子看看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());
说明:not1是否定返回值是单目的函数,std中还有not2它是否定返回值是双目的函数
例子需要包含头文件
#include <vector>
#include <algorithm>
#include <functional>
转载自:http://www.cnblogs.com/wanpengcoder/archive/2010/07/16/1778579.html
- STL中bind1st和bind2nd的区别
- stl 中bind1st和bind2nd
- bind1st 和 bind2nd的区别
- STL中bind1st和bind2nd之解析
- stl中 bind1st和 bind2nd 以及not1等适配器的使用
- STL binder1st binder2nd bind1st bind2nd区别
- STL bind1st、bind2nd的使用【转】
- STL: bind1st, bind2nd 的使用(C++)
- STL bind1st bind2nd详解
- STL bind1st bind2nd详解
- std::bind1st与std::bind2nd的区别
- 函数bind1st 和 bind2nd 的说明
- 解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- (转)解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- (转)解析bind1st和bind2nd的使用
- bind2nd 和 bind1st【转】
- ios推送的两篇文章
- CopyOnWriteArrayList
- Linux手动添加用户账号
- hadoop 安装出现的几种异常的处理方法,hadoop无法启动,no namenode to stop问题的解决方法,no datanode
- cocos2d-触摸分发原理
- STL中bind1st和bind2nd的区别
- TCP协议编程复习
- threadlocal和线程同步机制的比较
- iOS framework 制作教程
- 基于 Zen 创建一个 Drupal 7 的主题(模板)
- 简单 却爱
- php在windows2008中安装,使用IIS启动
- 将页面(元素)保存至客户端
- Writing device drivers in Linux: A brief tutorial(五)