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