stl中 bind1st和 bind2nd 以及not1等适配器的使用
来源:互联网 发布:淘宝买家信誉差会怎么 编辑:程序博客网 时间:2024/05/18 12:36
stl中 bind1st和 bind2nd 以及not1 等适配器的使用
可能这么解释以后大家还不是很清楚,那么就说点白话吧。
我们在做比较的时候所写的表达式像 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它是否定返回值是双目的函数。
- stl中 bind1st和 bind2nd 以及not1等适配器的使用
- 【STL】函数 for_each; bind1st和bind2nd,not1; mem_fun和mem_fun_ref;
- STL中bind1st和bind2nd的区别
- stl 中bind1st和bind2nd
- not1,not2,bind1st和bind2nd详解
- bind1st,bind2nd,not1,not2
- 关于函数适配器less< Type>以及bind1st和bind2nd的使用
- STL中bind1st和bind2nd之解析
- STL bind1st、bind2nd的使用【转】
- STL: bind1st, bind2nd 的使用(C++)
- bind1st bind2nd的使用
- bind1st bind2nd的使用
- bind1st bind2nd的使用
- bind1st bind2nd的使用
- 解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- (转)解析bind1st和bind2nd的使用
- 解析bind1st和bind2nd的使用
- c++堆栈详解
- Javascript 异步加载详解
- Android短信发送器的实现
- 技术债务真正的代价
- Android-----WebView使用
- stl中 bind1st和 bind2nd 以及not1等适配器的使用
- <cocos2d-x for wp7>在cocos2d-x使用spritesheet和用spritesheet创建动画
- Android(安卓)系统入门
- 5月8日十二周实验报告(二)采用多重继承派生新的类
- 梦入IBM之java基础第四天
- asp.net+jQuery(jPaginate插件)+AJAX 分页
- Windows Phone系统入门
- new Thread 时产生错误:Can't create handler inside thread that has not called Looper.prepare() 的解决方法
- 解决MacOS无法挂载NFS,Operation not permitted错误