function template
<functional>

std::bind1st

template <class Operation, class T>  binder1st<Operation> bind1st (const Operation& op, const T& x);
繫結第一個引數的函式物件
此函式使用二進位制函式物件op,透過將第一個引數繫結到固定值x來構造一個一元函式物件。

返回的函式物件bind1st定義了其operator(),使其只能接受一個引數。此引數用於呼叫二進位制函式物件op,並將x作為第一個引數的固定值。

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

1
2
3
4
5
template <class Operation, class T>
  binder1st<Operation> bind1st (const Operation& op, const T& x)
{
  return binder1st<Operation>(op, typename Operation::first_argument_type(x));
}

要將第二個引數繫結到特定值,請參閱 bind2nd

引數

op
派生自 binary_function 的二進位制函式物件。
x
op 的第一個引數的固定值。

返回值

等效於op但第一個引數始終設定為x的一元函式物件。
binder1st 是派生自 unary_function 的型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// bind1st example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;

int main () {
  int numbers[] = {10,20,30,40,50,10};
  int cx;
  cx = count_if (numbers, numbers+6, bind1st(equal_to<int>(),10) );
  cout << "There are " << cx << " elements that are equal to 10.\n";
  return 0;
}


There are 2 elements that are equal to 10.


另見