CppDS.com

C++ 98 11 14 17 20 手册

std::binary_negate

来自cppreference.com
< cpp‎ | utility‎ | functional
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等字符串转换
(C++17)
(C++17)
 
函数对象
函数包装
(C++11)
(C++11)
部分函数应用
(C++11)
(C++20)
函数调用
(C++17)
恒等函数对象
(C++20)
引用包装
(C++11)(C++11)
运算符包装
取反器
(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 前)
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 中移除)

binary_negate 是返回其所保有的二元谓词的补的包装函数对象。

二元谓词必须定义二个成员类型, first_argument_typesecond_argument_type ,可转换为谓词参数类型。从 std::owner_lessstd::refstd::crefstd::plusstd::minusstd::multipliesstd::dividesstd::modulusstd::equal_tostd::not_equal_tostd::greaterstd::lessstd::greater_equalstd::less_equalstd::logical_notstd::logical_orstd::bit_andstd::bit_orstd::bit_xorstd::mem_fnstd::map::value_compstd::multimap::value_compstd::function 或对 std::not2 调用获得的函数对象定义这些类型,和导出自弃用的 std::binary_function 的函数对象一样。

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 前)
explicit constexpr binary_negate( Predicate const& pred );
(C++14 起)

构造的 binary_negate 函数对象,拥有存储的谓词 pred

参数

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 <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;
    std::vector<int> v2;
    for (int i = 0; i < 10; ++i) v1.push_back(i);
    for (int i = 0; i < 10; ++i) v2.push_back(10 - i);
 
    std::vector<bool> v3(v1.size());
 
    std::binary_negate<same> not_same((same()));
 
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), not_same);
 
    /* C++11 解法:
        // 用 std::function<bool (int, int)>
        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 (int i = 0; i < 10; ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
}

输出:

0 10 true
1 9 true
2 8 true
3 7 true
4 6 true
5 5 false
6 4 true
7 3 true
8 2 true
9 1 true

参阅

(C++11 中弃用)(C++17 中移除)
与适配器兼容的二元函数基类
(类模板)
(C++11)
包装具有指定函数调用签名的任意类型的可调用对象
(类模板)
(C++17 中弃用)(C++20 中移除)
构造定制的 std::binary_negate 对象
(函数模板)
(C++11 中弃用)(C++17 中移除)
从函数指针创建与适配器兼容的函数对象包装器
(函数模板)
(C++17 中弃用)(C++20 中移除)
包装器函数对象,返回所持有的一元谓词的补
(类模板)
关闭