std::unary_function - cppreference.com
提供: cppreference.com
<tbody> </tbody>
|
|
(C++11で非推奨) (C++17で削除) |
|
unary_function は引数を1つ取る関数オブジェクトを作成するための基底クラスです。
unary_function は operator() を定義しません。 派生クラスがそれを定義することが期待されます。 unary_function はテンプレート引数によって定義される2つの型、 argument_type および result_type のみを提供します。
いくつかの標準ライブラリの関数オブジェクトアダプタ (std::not1 など) は、アダプトする関数オブジェクトが特定の型を定義していることを要求します。 std::not1 はアダプトする関数オブジェクトが argument_type という名前の型を持つことを要求します。 引数を1つ取る関数オブジェクトを unary_function から派生することはそれらのアダプタと互換性を持たせる簡単な方法です。
unary_function は C++11 で非推奨になりました。
メンバ型
| 型 | 定義 |
argument_type
|
ArgumentType
|
result_type
|
ResultType
|
例
#include <algorithm> #include <functional> #include <iostream> #include <vector> struct less_than_7 : std::unary_function<int, bool> { bool operator()(int i) const { return i < 7; } }; int main() { std::vector<int> v; for (int i = 0; i < 10; ++i) v.push_back(i); std::cout << std::count_if(v.begin(), v.end(), std::not1(less_than_7())); /* C++11 solution: // Cast to std::function<bool (int)> somehow - even with a lambda std::cout << std::count_if(v.begin(), v.end(), std::not1(std::function<bool (int)>([](int i){ return i < 7; })) ); */ }
出力: