命名空间
变体
操作

std::binary_negate

来自 cppreference.cn
 
 
 
函数对象
函数调用
(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*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
binary_negate
(直到 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

[编辑] 成员函数

(构造函数)
使用提供的谓词构造一个新的 binary_negate 对象
(公开成员函数)
operator()
返回对存储的谓词调用的结果的逻辑补码
(公开成员函数)

std::binary_negate::binary_negate

explicit binary_negate( Predicate const& pred );
(直到 C++14)
constexpr explicit binary_negate( Predicate const& pred );
(C++14 起)

构造一个带有存储谓词 predstd::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 - 要传递给谓词的第一个参数
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 solution:
    // 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 中已移除)
返回其所持一元谓词补集的包装函数对象
(类模板) [编辑]