命名空间
变体
操作

std::not2

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
 
函数对象
函数调用
(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*)
not2
(直到 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 >
std::binary_negate<Predicate> not2( const Predicate& pred );
(直到 C++14)
template< class Predicate >
constexpr std::binary_negate<Predicate> not2( const Predicate& pred );
(自 C++14 起)
(在 C++17 中已弃用)
(在 C++20 中移除)

std::not2 是一个辅助函数,用于创建一个函数对象,该对象返回传递的二元谓词函数的补码。创建的函数对象类型为 std::binary_negate<Predicate>

二元谓词类型必须定义两个成员类型,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 派生的函数对象也是如此。

目录

[编辑] 参数

pred - 二元谓词

[编辑] 返回值

std::not2 返回类型为 std::binary_negate<Predicate> 的对象,使用 pred 构造。

[编辑] 异常

(无)

[编辑] 示例

#include <algorithm>
#include <cstddef>
#include <functional>
#include <iostream>
#include <vector>
 
struct old_same : std::binary_function<int, int, bool>
{
    bool operator()(int a, int b) const { return a == b; }
};
 
struct new_same
{
    bool operator()(int a, int b) const { return a == b; }
};
 
bool same_fn(int a, int b)
{
    return a == b;
}
 
int main()
{
    std::vector<int> v1{0, 1, 2};
    std::vector<int> v2{2, 1, 0};
    std::vector<bool> v3(v1.size());
 
    std::cout << "negating a binary_function:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(old_same()));
 
    std::cout << std::boolalpha;
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
 
    std::cout << "negating a standard functor:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::equal_to<int>()));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
 
    std::cout << "negating a std::function:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::function<bool(int, int)>(new_same())));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
 
    std::cout << "negating a std::reference_wrapper:\n";
    std::transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),
                   std::not2(std::ref(same_fn)));
 
    for (std::size_t i = 0; i < v1.size(); ++i)
        std::cout << v1[i] << ' ' << v2[i] << ' ' << v3[i] << '\n';
}

输出

negating a binary_function:
0 2 true
1 1 false
2 0 true
negating a standard functor:
0 2 true
1 1 false
2 0 true
negating a std::function:
0 2 true
1 1 false
2 0 true
negating a std::reference_wrapper:
0 2 true
1 1 false
2 0 true

[编辑] 参见

(C++17)
创建一个函数对象,该对象返回它所持有的函数对象结果的补码
(函数模板) [编辑]
(在 C++17 中已弃用)(在 C++20 中移除)
包装器函数对象,返回它所持有的二元谓词的补码
(类模板) [编辑]
(C++11)
任何可复制构造的可调用对象的复制包装器
(类模板) [编辑]
任何可调用对象的仅移动包装器,该对象在给定的调用签名中支持限定符
(类模板) [编辑]
(在 C++17 中已弃用)(在 C++20 中移除)
构造自定义 std::unary_negate 对象
(函数模板) [编辑]
(在 C++11 中已弃用)(在 C++17 中移除)
从函数指针创建一个适配器兼容的函数对象包装器
(函数模板) [编辑]
(在 C++11 中已弃用)(在 C++17 中移除)
适配器兼容的二元函数基类
(类模板) [编辑]