function template
<functional>

std::bind2nd

template <class Operation, class T>  binder2nd<Operation> bind2nd (const Operation& op, const T& x);
返回第二個引數被繫結的函式物件
此函式透過將二元函式物件 op 的第二個引數繫結到固定值 x 來構造一個一元函式物件。

返回的函式物件bind2ndoperator()的定義方式是隻接受一個引數。此引數用於呼叫二元函式物件 op,並將 x 作為第二個引數的固定值。

它的定義與以下行為相同:

1
2
3
4
5
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 的型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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;
}


There are 3 negative elements.


另見