理解bind1st和bind2nd函数
来源:互联网 发布:java list map {} 编辑:程序博客网 时间:2024/05/20 03:44
值(v)是一个固定的参数。换言之,uf(x)等价于:
- bf( x, v) – 当使用bind2nd时
- bf( v, x) – 当使用bind1st时
bind1st和bind2nd函数在处理谓词时非常有用。它们使得二元谓词能够转换成一元谓词;这常用于将一个范围内的所有值与一个特定的值比较:
std::vector< int> a;
// ……填充a
// 移除所有小于30的元素
a.erase( std::remove_if( a.begin(), a.end(),
std::bind2nd( std::less< int>(), 30)), a.end());
在大多数时候,bind2nd就足够了,像上面的例子。不管怎样,在进行泛型编程时,你会实现一些处理谓词的函数。谓词指定了范围内的排序准则,通常是“<”(std::less< type>)。记住你可以仅用一个给定的“<”运算符来实现“<=”、“<=”和“>”运算符。这时你会发现bind1st和bind2nd都能用上。
#include <iostream>
#include <algorithm>
#include <functional>
template< class iterator, class predicate, class doer>
void for_each_if( iterator itFirst, iterator itLast, predicate pred, doer do_it)
{
while ( itFirst != itLast)
{
if ( pred( *itFirst)) do_it( *itFirst);
++itFirst;
}
}
void print( int i) { std::cout << i << " "; }
int main(int argc, char* argv[])
{
int aNumbers[] = { 10, 5, 89, 9, 30, -2, -8, 7, 33, 25, 30, 76, 0, 2};
int nCount = sizeof( aNumbers) / sizeof( aNumbers[ 0]);
// a < b
std::cout << "/nNumbers less than 30: ";
for_each_if( aNumbers, aNumbers + nCount,
std::bind2nd( std::less< int>(), 30), print);
std::cout << "/nNumbers bigger than 30: ";
// a > b
for_each_if( aNumbers, aNumbers + nCount,
std::bind1st( std::less< int>(), 30), print);
std::cout << "/nNumbers less or equal than 30: ";
// a <= b <=> !(a > b)
for_each_if( aNumbers, aNumbers + nCount,
std::not1( std::bind1st( std::less< int>(), 30)), print);
std::cout << "/nNumbers bigger or equal than 30: ";
// a >= b <=> !(a < b)
for_each_if( aNumbers, aNumbers + nCount,
std::not1( std::bind2nd( std::less< int>(), 30)), print);
return 0;
}
- 理解bind1st和bind2nd函数
- bind1st和bind2nd函数
- 函数bind1st 和 bind2nd 的说明
- bind2nd 和 bind1st【转】
- bind1st 和 bind2nd 解析
- bind1st和bind2nd
- 【STL】函数 for_each; bind1st和bind2nd,not1; mem_fun和mem_fun_ref;
- 二元函数与bind1st/bind2nd
- 函数对象适配器 bind2nd bind1st
- bind1st与bind2nd函数解析
- bind1st与bind2nd函数解析
- bind1st和bind2nd这两个函数的用法。
- bind1st和bind2nd这两个函数的用法
- bind1st和bind2nd函数用于将一个二元算子
- bind1st 和 bind2nd的区别
- stl 中bind1st和bind2nd
- bind1st & bind2nd
- 【STL】bind1st与bind2nd函数解析
- Windows下C语言网络编程快速入门
- 关于IObjectSafety的思考
- JFace Viewer final
- 通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行
- 用foobar将ape转换为mp3格式
- 理解bind1st和bind2nd函数
- linux下java环境变量的设置
- 在线编辑器FCKeditor 2.0试用小记
- EXCEL/ACCESS数据转换
- 哈哈,开张大吉~~
- 第3回 软件测试和质量保证的关系
- 顺序表SqList的C++代码实现
- 初识MYSQL(安装)
- JAVA命名规范