Namespaces
Variants

std::binary_negate

来自 zh.cppreference.net
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧版绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 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 );
(C++14 起为 constexpr)

构造一个std::binary_negate函数对象,其中包含存储的谓词pred。

参数

pred - 谓词函数对象

std::binary_negate::operator()

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

返回调用 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 中移除)
包装函数对象,返回其持有的一元谓词的补集
(类模板)