Namespaces
Variants

std::not1

从 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 >
std::unary_negate<Predicate> not1( const Predicate& pred );
(直到 C++14)
template< class Predicate >
constexpr std::unary_negate<Predicate> not1( const Predicate& pred );
(自 C++14)
(C++17 中弃用)
(C++20 中移除)

std::not1 是一个辅助函数,用于创建一个函数对象,该对象返回传入的一元谓词函数的补集。所创建的函数对象的类型为 std::unary_negate<Predicate>。

一元谓词类型必须定义一个成员类型 argument_type,该类型可转换至谓词的参数类型。从 std::ref、std::cref、std::negate、std::logical_not、std::mem_fn、std::function、std::hash,或从另一次对 std::not1 的调用获得的一元函数对象都定义有此类型,派生自已弃用的 std::unary_function 的函数对象也是如此。

参数

pred - 一元谓词

返回值

std::not1 返回一个类型为 std:: unary_negate < Predicate > 的对象,该对象通过 pred 构造。

示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
struct LessThan7 : std::unary_function<int, bool>
{
    bool operator()(int i) const { return i < 7; }
};
int main()
{
    std::vector<int> v(10);
    std::iota(std::begin(v), std::end(v), 0);
    std::cout << std::count_if(begin(v), end(v), std::not1(LessThan7())) << '\n';
    // 使用 std::function 实现相同功能
    std::function<bool(int)> less_than_9 = [](int x) { return x < 9; };
    std::cout << std::count_if(begin(v), end(v), std::not1(less_than_9)) << '\n';
}

输出:

3
1

参见

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