Namespaces
Variants

std:: not1

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* )
not1
( until C++20* )
( 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:: 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)
适配器兼容的一元函数基类
(类模板)