std::unary_function
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <functional> で定義
|
||
template <typename ArgumentType, typename ResultType> struct unary_function; |
(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
|
例
Run this code
#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; }))
);
*/
}
出力:
3
関連項目
(C++11) |
指定された関数呼び出しシグネチャを持つ任意の型の呼び出し可能なオブジェクトをラップします (クラステンプレート) |
(C++11で非推奨)(C++17で削除) |
関数ポインタからアダプタ互換な関数オブジェクトを作成します (関数テンプレート) |
(C++11で非推奨)(C++17で削除) |
単項関数へのポインタに対するアダプタ互換なラッパー (クラステンプレート) |
(C++11で非推奨)(C++17で削除) |
アダプタ互換な二項関数の基底クラス (クラステンプレート) |