std::unary_negate
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <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
是一个包装器函数对象,返回它所持有的unary谓词的补码。
unary谓词类型必须定义一个成员类型 argument_type
,它可以转换为谓词的参数类型。从 std::ref
、std::cref
、std::negate
、std::logical_not
、std::mem_fn
、std::function
、std::hash
获取的unary函数对象,或者从另一个 std::not1
调用获取的unary函数对象,都定义了这个类型,从已弃用的 std::unary_function
派生的函数对象也是如此。
std::unary_negate
对象可以使用辅助函数 std::not1
轻松构造。
目录 |
[编辑] 成员类型
类型 | 定义 |
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 中已移除) |
包装器函数对象,返回它所持有的 binary 谓词的补码 (类模板) |
(C++11) |
任何可复制构造的可调用对象的 copyable 包装器 (类模板) |
(C++23) |
任何支持给定调用签名的限定符的可调用对象的 move-only 包装器 (类模板) |
(C++17 中已弃用)(C++20 中已移除) |
构造自定义的 std::unary_negate 对象(函数模板) |
(C++11 中已弃用)(C++17 中已移除) |
从函数指针创建适配器兼容的函数对象包装器 (函数模板) |
(C++11 中已弃用)(C++17 中已移除) |
适配器兼容的 unary 函数基类 (类模板) |