類模板
<functional>
std::binder1st
template <class Operation> class binder1st;
生成綁定了第一個引數的函式物件類
透過繫結第一個引數為一個固定值,從二元物件類
Operation生成一個一元函式物件類。
binder1st通常用作一個型別。函式
bind1st(也在標頭檔案
<functional> 中定義)可用於直接構造該型別的物件。
binder1st是透過將
二元函式物件 作為引數來構造的。該物件的副本由其成員
operator()使用,該成員函式利用其引數和構造時設定的固定值生成結果。
該類派生自
unary_function,通常定義為
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
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(它需要成員
first_argument_type和
second_argument_type).
成員
- 建構函式
- 透過繫結第一個引數為一個值,從二元函式物件構造一元函式物件類。
- operator()
- 成員函式接收一個引數,並返回呼叫在構造時使用的二元函式物件(第一個引數已繫結到特定值)的結果。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// 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.
|
另見
- bind1st
- 返回第一個引數被繫結的函式物件 (函式模板)
- binder2nd
- 生成第二個引數被繫結的函式物件類 (類模板)
- unary_function
- 一元函式物件基類 (類模板)
- binary_function
- 二元函式物件基類 (類模板)