std::not1
来自 cppreference.cn
定义于头文件 <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'; // 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 中已移除) |
返回其所持一元谓词补集的包装函数对象 (类模板) |
(C++11) |
任何可复制构造的可调用对象的包装器 (类模板) |
(C++23) |
任何支持给定调用签名中限定符的可调用对象的仅移动包装器 (类模板) |
(C++17 中已弃用)(C++20 中已移除) |
构造自定义的 std::binary_negate 对象 (函数模板) |
(C++11 中已废弃)(C++17 中已移除) |
从函数指针创建适配器兼容函数对象包装器 (函数模板) |
(C++11 中已废弃)(C++17 中已移除) |
与适配器兼容的一元函数基类 (类模板) |