Namespaces
Variants

std:: binary_negate

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* )
( 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* )
binary_negate
( until C++20* )
ヘッダーで定義 <functional>
template < class Predicate >

struct binary_negate
: public std:: binary_function <
Predicate :: first_argument_type ,
Predicate :: second_argument_type ,
bool

> ;
(C++11まで)
template < class Predicate >
struct binary_negate ;
(C++11から)
(C++17で非推奨)
(C++20で削除)

std::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 から派生した関数オブジェクトも同様です。

std::binary_negate オブジェクトは、ヘルパー関数 std::not2 を使用して簡単に構築できます。

目次

メンバー型

定義
first_argument_type Predicate :: first_argument_type
second_argument_type Predicate :: second_argument_type
result_type bool

メンバー関数

(constructor)
指定された述語で新しいbinary_negateオブジェクトを構築する
(public member function)
operator()
格納された述語の呼び出し結果の論理否定を返す
(public member function)

std::binary_negate:: binary_negate

explicit binary_negate ( Predicate const & pred ) ;
(C++14まで)
constexpr explicit binary_negate ( Predicate const & pred ) ;
(C++14以降)

格納された述語 pred を持つ std::binary_negate 関数オブジェクトを構築します。

パラメータ

pred - 述語関数オブジェクト

std::binary_negate:: operator()

bool operator ( ) ( first_argument_type const & x,
second_argument_type const & y ) const ;
(C++14まで)
constexpr bool 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 <cstddef>
#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;
    for (int i = 0; i < 7; ++i)
        v1.push_back(i);
    std::vector<int> v2(v1.size());
    std::reverse_copy(v1.begin(), v1.end(), v2.begin());
    std::vector<bool> v3(v1.size());
    std::binary_negate<same> not_same((same()));
    // C++11 ソリューション:
    // 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 (std::size_t i = 0; i != v1.size(); ++i)
        std::cout << v1[i] << " != " << v2[i] << " : " << v3[i] << '\n';
}

出力:

0 != 6 : true
1 != 5 : true
2 != 4 : true
3 != 3 : false
4 != 2 : true
5 != 1 : true
6 != 0 : true

関連項目

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