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 是一个包装函数对象,返回其持有的二元谓词的逻辑补集。

二元谓词类型必须定义两个成员类型: 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 对象
(公开成员函数)
operator()
返回对存储谓词调用结果的逻辑补集
(公开成员函数)

std::binary_negate:: binary_negate

explicit binary_negate ( Predicate const & pred ) ;
(until C++14)
constexpr explicit binary_negate ( Predicate const & pred ) ;
(since C++14)

构造一个 std::binary_negate 函数对象,其存储的谓词为 pred

参数

pred - 谓词函数对象

std::binary_negate:: operator()

bool operator ( ) ( first_argument_type const & x,
second_argument_type const & y ) const ;
(until C++14)
constexpr bool operator ( ) ( first_argument_type const & x,
second_argument_type const & y ) const ;
(since C++14)

返回调用 pred ( x, y ) 结果的逻辑补值。

参数

x - 传递给谓词的第一参数
y - 传递给谓词的第二参数

返回值

调用 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

参见

(C++11 中弃用) (C++17 中移除)
适配器兼容的二元函数基类
(类模板)
(C++11)
任何可复制构造的可调用对象的包装器
(类模板)
仅移动包装器,适用于支持给定调用签名中限定符的任何可调用对象
(类模板)
(C++17 中弃用) (C++20 中移除)
构造自定义的 std::binary_negate 对象
(函数模板)
(C++11 中弃用) (C++17 中移除)
从函数指针创建适配器兼容的函数对象包装器
(函数模板)
(C++17 中弃用) (C++20 中移除)
包装函数对象,返回其持有的一元谓词的补集
(类模板)