std::unary_negate
提供: cppreference.com
< cpp | utility | functional
ヘッダ <functional> で定義 | ||
template<class Predicate > struct unary_negate :publicstd::unary_function<Predicate::argument_type, bool>; | (C++11未満) | |
template<class Predicate > struct unary_negate; | (C++11以上) (C++17で非推奨) (C++20で削除) | |
unary_negate
は保持する単項述語の否定を返すラッパー関数オブジェクトです。
単項述語の型は、その述語の引数の型に変換可能なメンバ型 argument_type
を定義しなければなりません。 std::ref、 std::cref、 std::negate、 std::logical_not、 std::mem_fn、 std::function、 std::hash から、および std::not1 の別の呼び出しから取得された単項関数オブジェクトは、非推奨な std::unary_function から派生した関数オブジェクトのように、その型を定義します。
unary_negate
オブジェクトはヘルパー関数 std::not1 で簡単に構築できます。
目次 |
[編集]メンバ型
型 | 定義 |
argument_type | Predicate::argument_type |
result_type | bool |
[編集]メンバ関数
コンストラクタ | 供給された述語を持つ新しい unary_negate オブジェクトを構築します (パブリックメンバ関数) |
operator() | 格納されている述語の呼び出しの結果の論理否定を返します (パブリックメンバ関数) |
std::unary_negate::unary_negate
explicit unary_negate( Predicate const& pred ); | (C++14未満) | |
explicitconstexpr unary_negate( Predicate const& pred ); | (C++14以上) | |
格納される述語 pred
を持つ unary_negate
関数オブジェクトを構築します。
引数
pred | - | 述語の関数オブジェクト |
std::unary_negate::operator()
bool operator()( argument_type const& x )const; | (C++14未満) | |
constexprbool operator()( argument_type const& x )const; | (C++14以上) | |
pred(x) を呼んだ結果の論理否定を返します。
引数
x | - | 述語に渡す引数 |
戻り値
pred(x) を呼んだ結果の論理否定。
[編集]例
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::unary_negate<less_than_7> not_less_than_7((less_than_7())); std::cout<<std::count_if(v.begin(), v.end(), not_less_than_7); /* C++11 solution: // Use std::function<bool (int)> std::function<bool (int)> not_less_than_7 = [](int x)->bool{ return !less_than_7()(x); }; std::cout << std::count_if(v.begin(), v.end(), not_less_than_7); */}
出力:
3
[編集]関連項目
(C++17で非推奨)(C++20で削除) | 保持する二項述語の否定を返すラッパー関数オブジェクト (クラステンプレート) |
(C++11) | 指定された関数呼び出しシグネチャを持つ任意の型の呼び出し可能なオブジェクトをラップします (クラステンプレート) |
(C++17で非推奨)(C++20で削除) | カスタム std::unary_negate オブジェクトを構築します (関数テンプレート) |
(C++11で非推奨)(C++17で削除) | 関数ポインタからアダプタ互換な関数オブジェクトを作成します (関数テンプレート) |
(C++11で非推奨)(C++17で削除) | アダプタ互換な単項関数の基底クラス (クラステンプレート) |