類模板
<functional>

std::pointer_to_unary_function

template <class Arg, class Result> class pointer_to_unary_function;
從函式指標生成一元函式物件類
從接受單個引數 Arg 型別並返回 Result 型別值的函式指標生成一元函式物件類。

pointer_to_unary_function通常用作一個型別。函式 ptr_fun (也在標頭檔案 <functional> 中定義) 可用於直接構造此型別的物件。

該類派生自unary_function,通常定義為

1
2
3
4
5
6
7
8
9
10
template <class Arg, class Result>
  class pointer_to_unary_function : public unary_function <Arg,Result>
{
protected:
  Result(*pfunc)(Arg);
public:
  explicit pointer_to_unary_function ( Result (*f)(Arg) ) : pfunc (f) {}
  Result operator() (Arg x) const
    { return pfunc(x); }
};

成員

建構函式
從接受單個引數 Arg 型別並返回 Result 型別值的函式指標構造一元函式物件類。
operator()
成員函式,接受單個引數,並返回呼叫構造時使用的函式指標的結果。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// pointer_to_unary_function example
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
using namespace std;

int main () {
  pointer_to_unary_function <double,double> LogObject (log);
  double numbers[] = {10.0, 20.0, 40.0, 80.0, 160.0};
  double logs[5];
  transform (numbers, numbers+5, logs, LogObject);
  for (int i=0; i<5; i++)
    cout << logs[i] << " ";
  cout << endl;
  return 0;
}

可能的輸出

2.30259 2.99573 3.68888 4.38203 5.07517


另見