std::
binder2nd
类模板 <functional>
template <class Operation> class binder2nd;
|
生成绑定第二个参数的函数对象类
通过将二元对象类Operation的第二个参数绑定到一个固定值,生成一元函数对象类。
binder2nd通常用作类型。函数bind2nd(也在header <functional>中定义)可以用来直接构造这种类型的对象。
binder2nd使用一个二元函数对象作为参数构造。
该对象的成员operator()使用该对象的副本根据其形参和构造时设置的固定值生成结果。
该类派生自unary_function,通常定义为:
C++98 |
template <class Operation> class binder2nd
: public unary_function <typename Operation::first_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::second_argument_type value;
public:
binder2nd ( const Operation& x,
const typename Operation::second_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::first_argument_type& x) const
{ return op(x,value); }
};
|
binder2nd类专门用于绑定从binary_function派生的函数对象(operations)
(它需要成员first_argument_type和second_argument_type)。
☲ 成员
- constructor
- 通过将二元函数对象的第二个参数绑定到值来构造一元函数对象类
- operator()
- 成员函数接受单个形参,返回调用构造时使用的二元函数对象的结果,其第二个实参绑定到一个特定值
☣ 示例
// binder2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main () {
binder2nd < less<int> > IsNegative (less<int>(),0);
int numbers[] = {10,-20,-30,40,-50};
int cx;
cx = count_if (numbers,numbers+5,IsNegative);
cout << "There are " << cx << " negative elements.\n";
return 0;
}
|
输出:
There are 3 negative elements.
🍄 另请参阅