`
阿尔萨斯
  • 浏览: 4170314 次
社区版块
存档分类
最新评论

引用 bind1st bind2nd

 
阅读更多

本篇适合不熟悉这两个函数的读者 以前在使用stl的过程中发现bind1st和bind2nd这两个函数,当时不太理解什么意思,今天在网上查了一下相关资料发现竟然很简单,下面我就具体解释一下他们的用法。 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 #include #include ___________________________________________________________________________ bind1st和bind2nd函数用于将一个二元算子(binary functor,bf)转换成一元算子(unary functor,uf)。为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v)。 被过滤广告 值(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 #include #include 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; } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chenhu_doc/archive/2006/07/29/999504.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics