次の方法で共有


unary_function 構造体

派生クラスによって継承される可能性のある型を定義する空の基本構造体単項関数オブジェクトを提供できます。

template<class Arg, class Result> 
   struct unary_function { 
      typedef Arg argument_type; 
      typedef Result result_type; 
   };

解説

テンプレートの構造体はフォーム result_type operator() (const argument_type&) のメンバー関数を定義する constクラスの基本クラスとして機能します。

このような派生の単項関数はすべて argument_type として唯一の引数の型と result_typeとして戻り値の型を指定できます。

使用例

// functional_unary_function.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>

using namespace std;

// Creation of a user-defined function object
// that inherits from the unary_function base class
class greaterthan10: unary_function<int, bool>
{
public:
    result_type operator()(argument_type i)
    {
        return (result_type)(i > 10);
    }
};

int main()
{
    vector<int> v1;
    vector<int>::iterator Iter;

    int i;
    for (i = 0; i <= 5; i++)
    {
        v1.push_back(5 * i);
    }

    cout << "The vector v1 = ( " ;
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
        cout << *Iter << " ";
    cout << ")" << endl;

    vector<int>::iterator::difference_type result1;
    result1 = count_if(v1.begin(), v1.end(), greaterthan10());
    cout << "The number of elements in v1 greater than 10 is: "
         << result1 << "." << endl;
}
  

必要条件

ヘッダー: <functional>

名前空間: std

参照

関連項目

unary_function<> 構造体

C++ 標準ライブラリ内のスレッド セーフ

標準テンプレート ライブラリ