Namespaces
Variants

std:: not2

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
not2
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
定義済みヘッダ <functional>
template < class Predicate >
std:: binary_negate < Predicate > not2 ( const Predicate & pred ) ;
(C++14まで)
template < class Predicate >
constexpr std:: binary_negate < Predicate > not2 ( const Predicate & pred ) ;
(C++14から)
(C++17で非推奨)
(C++20で削除)

std::not2 は、渡された二項述語関数の補数を返す関数オブジェクトを作成するためのヘルパー関数です。作成される関数オブジェクトは std:: binary_negate < Predicate > 型です。

二項述語の型は、述語のパラメータ型に変換可能な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 から派生した関数オブジェクトも同様です。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、リンク先は一切変更していません - C++関連の用語(Parameters、Return value、Exceptions、Example、See also)は原文のまま保持しています - 数値や書式設定は完全に維持しています

パラメータ

pred - 二項述語

戻り値

std::not2 std:: binary_negate < Predicate > 型のオブジェクトを返し、これは pred で構築されます。

例外

(なし)

#include <algorithm>
#include <cstddef>
#include <functional>
#include <iostream>
#include <vector>
struct old_same : std::binary_function<int, int, bool>
{
    bool operator()(int a, int b) const { return a == b; }
};
struct new_same
{
    bool operator()(int a, int b) const { return a == b; }
};
bool same_fn(int a, int b)
{
    return a == b;
}
int main()
{
    std::vector<int> v1{0, 1, 2};
    std::vector<int> v2{2, 1, 0};
    std::vector<bool> v3(v1.size());
    std::cout << "二項関数の否定:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(old_same()));
    std::cout << std::boolalpha;
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
    std::cout << "標準ファンクタの否定:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::equal_to<int>()));
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
    std::cout << "std::functionの否定:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::function<bool(int, int)>(new_same())));
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
    std::cout << "std::reference_wrapperの否定:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::ref(same_fn)));
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
}

出力:

二項関数の否定:
0 2 true
1 1 false
2 0 true
標準ファンクタの否定:
0 2 true
1 1 false
2 0 true
std::functionの否定:
0 2 true
1 1 false
2 0 true
std::reference_wrapperの否定:
0 2 true
1 1 false
2 0 true

関連項目

(C++17)
保持している関数オブジェクトの結果の補数を返す関数オブジェクトを作成する
(関数テンプレート)
(C++17で非推奨) (C++20で削除)
保持している二項述語の補数を返すラッパー関数オブジェクト
(クラステンプレート)
(C++11)
コピー構築可能な任意の呼び出し可能オブジェクトのコピー可能ラッパー
(クラステンプレート)
特定の呼び出しシグネチャで修飾子をサポートする任意の呼び出し可能オブジェクトのムーブ専用ラッパー
(クラステンプレート)
(C++17で非推奨) (C++20で削除)
カスタム std::unary_negate オブジェクトを構築する
(関数テンプレート)
(C++11で非推奨) (C++17で削除)
関数ポインタからアダプタ互換の関数オブジェクトラッパーを作成する
(関数テンプレート)
(C++11で非推奨) (C++17で削除)
アダプタ互換な二項関数の基底クラス
(クラステンプレート)