std::unary_negate
来自 cppreference.cn
| 定义于头文件 <functional> |
||
| template< class Predicate > struct unary_negate : public std::unary_function<Predicate::argument_type, bool>; |
(C++11 前) | |
| template< class Predicate > struct unary_negate; |
(C++11 起) (C++17 中已弃用) (C++20 中移除) |
|
std::unary_negate 是一个包装函数对象,返回其持有的一元谓词的补码。
一元谓词类型必须定义一个成员类型 `argument_type`,它可转换为谓词的参数类型。从 std::ref、std::cref、std::negate、std::logical_not、std::mem_fn、std::function、std::hash 或另一次调用 std::not1 获得的函数对象都定义了此类型,从已弃用的 std::unary_function 派生的函数对象也是如此。
使用辅助函数 std::not1 可以轻松构造 `std::unary_negate` 对象。
目录 |
[编辑] 成员类型
| 类型 | 定义 |
argument_type
|
Predicate::argument_type |
result_type
|
bool |
[编辑] 成员函数
| (构造函数) |
构造一个新的 unary_negate 对象,并带有所提供的谓词 (公开成员函数) |
| operator() |
返回对存储的谓词调用的结果的逻辑补码 (公开成员函数) |
std::unary_negate::unary_negate
| explicit unary_negate( Predicate const& pred ); |
(直到 C++14) | |
| constexpr explicit unary_negate( Predicate const& pred ); |
(C++14 起) | |
构造一个带有存储谓词 pred 的 `std::unary_negate` 函数对象。
参数
| pred | - | 谓词函数对象 |
std::unary_negate::operator()
| bool operator()( argument_type const& x ) const; |
(直到 C++14) | |
| constexpr bool operator()( argument_type const& x ) const; |
(C++14 起) | |
返回调用 pred(x) 结果的逻辑补码。
参数
| x | - | 要传递给谓词的参数 |
返回值
调用 pred(x) 结果的逻辑补码。
[编辑] 示例
运行此代码
#include <algorithm> #include <functional> #include <iostream> #include <vector> struct less_than_7 : std::unary_function<int, bool> { bool operator()(int i) const { return i < 7; } }; int main() { std::vector<int> v(7, 7); v[0] = v[1] = v[2] = 6; std::unary_negate<less_than_7> not_less_than_7((less_than_7())); // C++11 solution: // Use std::function<bool (int)> // std::function<bool (int)> not_less_than_7 = // [](int x)->bool { return !less_than_7()(x); }; std::cout << std::count_if(v.begin(), v.end(), not_less_than_7); }
输出
4
[编辑] 参阅
| (C++17 中已弃用)(C++20 中已移除) |
返回其所持二元谓词补集的包装函数对象 (类模板) |
| (C++11) |
任何可复制构造的可调用对象的包装器 (类模板) |
| (C++23) |
任何支持给定调用签名中限定符的可调用对象的仅移动包装器 (类模板) |
| (C++17 中已弃用)(C++20 中已移除) |
构造自定义 std::unary_negate 对象 (函数模板) |
| (C++11 中已废弃)(C++17 中已移除) |
从函数指针创建适配器兼容函数对象包装器 (函数模板) |
| (C++11 中已废弃)(C++17 中已移除) |
与适配器兼容的一元函数基类 (类模板) |