bind2nd

bind2nd

template <class Operation,class T>
  binder2nd <Operation> bind2nd(const Operation&op,const T&x);

返回第二个参数绑定的函数对象

此函数通过将其第二个参数绑定到固定值x,从二元函数对象op构造一元函数对象。bind2nd 返回的函数对象定义了operator(),只需要一个参数。此参数用于调用二进制函数对象op,其中x为第二个参数的固定值。 它的定义与以下行为相同:

template <class Operation, class T>
  binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
  return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}

要将第一个参数绑定到特定值,请参阅bind1st.

参数

  • op

    二进制函数对象派生自binary_function

  • x

    op的第二个参数的固定值。

返回值

一个等效于op的一元函数对象,但第二个参数始终设置为x
binder2nd是从unary_function派生的类型。

// bind2nd example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  int numbers[] = {10,-20,-30,40,-50};
  int cx;
  cx = count_if ( numbers, numbers+5, bind2nd(less<int>(),0) );
  cout << "There are " << cx << " negative elements.\n";
  return 0;
}
有3个负面因素。

See also

bind1st Return function object with first parameter bound (function template )
binder2nd Generate function object class with 2nd parameter bound (class template )
unary_function Unary function object base class (class template )
binary_function Binary function object base class (class template )
posted @ 2019-08-19 21:31  刺客伍六七  阅读(822)  评论(0编辑  收藏  举报