std::binary_negate
ヘッダ <functional> で定義 | ||
template<class Predicate > struct binary_negate : | (C++11未満) | |
template<class Predicate > struct binary_negate; | (C++11以上) (C++17で非推奨) (C++20で削除) | |
binary_negate
は保持する二項述語の否定を返すラッパー関数オブジェクトです。
二項述語の型は、その述語の引数の型に変換可能な2つのメンバ型 first_argument_type
および second_argument_type
を定義しなければなりません。 std::owner_less、 std::ref、 std::cref、 std::plus、 std::minus、 std::multiplies、 std::divides、 std::modulus、 std::equal_to、 std::not_equal_to、 std::greater、 std::less、 std::greater_equal、 std::less_equal、 std::logical_not、 std::logical_or、 std::bit_and、 std::bit_or、 std::bit_xor、 std::mem_fn、 std::map::value_comp、 std::multimap::value_comp、 std::function から、および std::not2 の呼び出しから取得した関数オブジェクトは、非推奨の std::binary_function から派生した関数オブジェクトのように、それらの型を定義します。
binary_negate
オブジェクトはヘルパー関数 std::not2 で簡単に構築できます。
目次 |
[編集]メンバ型
型 | 定義 |
first_argument_type | Predicate::first_argument_type |
second_argument_type | Predicate::second_argument_type |
result_type | bool |
[編集]メンバ関数
コンストラクタ | 供給された述語を持つ新しい binary_negate オブジェクトを構築します (パブリックメンバ関数) |
operator() | 格納されている述語の呼び出しの結果の論理否定を返します (パブリックメンバ関数) |
std::binary_negate::binary_negate
explicit binary_negate( Predicate const& pred ); | (C++14未満) | |
explicitconstexpr binary_negate( Predicate const& pred ); | (C++14以上) | |
格納される述語 pred
を持つ binary_negate
関数オブジェクトを構築します。
引数
pred | - | 述語の関数オブジェクト |
std::binary_negate::operator()
bool operator()( first_argument_type const& x, second_argument_type const& y )const; | (C++14未満) | |
constexprbool operator()( first_argument_type const& x, second_argument_type const& y )const; | (C++14以上) | |
pred(x, y) を呼んだ結果の論理否定を返します。
引数
x | - | 述語に渡す第1引数 |
y | - | 述語に渡す第2引数 |
戻り値
pred(x, y) を呼んだ結果の論理否定。
[編集]例
#include <algorithm>#include <functional>#include <iostream>#include <vector> struct same :std::binary_function<int, int, bool>{bool operator()(int a, int b)const{return a == b;}}; int main(){std::vector<int> v1;std::vector<int> v2;for(int i =0; i <10;++i) v1.push_back(i);for(int i =0; i <10;++i) v2.push_back(10- i); std::vector<bool> v3(v1.size()); std::binary_negate<same> not_same((same())); std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same); /* C++11 solution: // Use std::function<bool (int, int)> std::function<bool (int, int)> not_same = [](int x, int y)->bool{ return !same()(x, y); }; std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same); */ std::cout.setf(std::ios_base::boolalpha);for(int i =0; i <10;++i)std::cout<< v1[i]<<' '<< v2[i]<<' '<< v3[i]<<'\n';}
出力:
0 10 true 1 9 true 2 8 true 3 7 true 4 6 true 5 5 false 6 4 true 7 3 true 8 2 true 9 1 true
[編集]関連項目
(C++11で非推奨)(C++17で削除) | アダプタ互換な二項関数の基底クラス (クラステンプレート) |
(C++11) | 指定された関数呼び出しシグネチャを持つ任意の型の呼び出し可能なオブジェクトをラップします (クラステンプレート) |
(C++17で非推奨)(C++20で削除) | カスタム std::binary_negate オブジェクトを構築します (関数テンプレート) |
(C++11で非推奨)(C++17で削除) | 関数ポインタからアダプタ互換な関数オブジェクトを作成します (関数テンプレート) |
(C++17で非推奨)(C++20で削除) | 保持する単項述語の否定を返すラッパー関数オブジェクト (クラステンプレート) |