std::not1
来自cppreference.com
| 在标头 <functional> 定义
|
||
| |
(C++14 前) | |
| |
(C++14 起) (C++17 弃用) (C++20 移除) |
|
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 返回以 pred 构造的 std::unary_negate<Predicate> 类型的对象。
异常
(无)
示例
运行此代码
#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) |
创建返回其保有的函数对象的结果之补的函数对象 (函数模板) |
(C++17 弃用)(C++20 移除) |
包装器函数对象,返回所持有的一元谓词的补 (类模板) |
(C++11) |
任意可复制构造的可调用对象的可复制包装 (类模板) |
(C++23) |
任意可调用对象的仅移动包装,支持给定调用签名中的限定符 (类模板) |
(C++17 弃用)(C++20 移除) |
构造定制的 std::binary_negate 对象 (函数模板) |
(C++11 弃用)(C++17 移除) |
从函数指针创建与适配器兼容的函数对象包装器 (函数模板) |
(C++11 弃用)(C++17 移除) |
与适配器兼容的一元函数基类 (类模板) |