Namespaces
Variants

std:: not2

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* )
not2
( 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* )
( until 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_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 派生的函数对象也是如此。

目录

参数

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 << "对二元函数取反:\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 << "对标准函数对象取反:\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 << "对 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 << "对 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<span class

参见

(C++17)
创建返回其所持有函数对象结果之补集的函数对象
(函数模板)
(deprecated in C++17) (removed in C++20)
返回其所持有二元谓词之补集的包装函数对象
(类模板)
(C++11)
任意可复制构造可调用对象的可复制包装器
(类模板)
支持给定调用签名中限定符的任意可调用对象的仅移动包装器
(类模板)
(deprecated in C++17) (removed in C++20)
构造自定义的 std::unary_negate 对象
(函数模板)
(deprecated in C++11) (removed in C++17)
从函数指针创建适配器兼容的函数对象包装器
(函数模板)
(deprecated in C++11) (removed in C++17)
适配器兼容的二元函数基类
(类模板)