命名空间
变体
操作

std::not1

来自 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*)
not1
(直到 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 是一个辅助函数,用于创建一个函数对象,该对象返回传递的 unary 谓词函数的补码。创建的函数对象类型为 std::unary_negate<Predicate>

unary 谓词类型必须定义一个成员类型 argument_type,该类型可转换为谓词的参数类型。从 std::refstd::crefstd::negatestd::logical_notstd::mem_fnstd::functionstd::hash 或另一次调用 std::not1 获得的 unary 函数对象都定义了此类型,从已弃用的 std::unary_function 派生的函数对象也是如此。

目录

[编辑] 参数

pred - unary 谓词

[编辑] 返回值

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';
 
    // the same as above using 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)
创建一个函数对象,该对象返回它所持有的函数对象结果的补码
(函数模板) [编辑]
(在 C++17 中弃用)(在 C++20 中移除)
包装函数对象,返回它所持有的 unary 谓词的补码
(类模板) [编辑]
(C++11)
任何可复制构造的可调用对象的 copyable 包装器
(类模板) [编辑]
任何可调用对象的 move-only 包装器,该对象在给定的调用签名中支持限定符
(类模板) [编辑]
(在 C++17 中弃用)(在 C++20 中移除)
构造自定义的 std::binary_negate 对象
(函数模板) [编辑]
(在 C++11 中弃用)(在 C++17 中移除)
从函数指针创建一个适配器兼容的函数对象包装器
(函数模板) [编辑]
(在 C++11 中弃用)(在 C++17 中移除)
适配器兼容的 unary 函数基类
(类模板) [编辑]