std::unary_negate
来自cppreference.com
< 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
是返回其所保有的一元谓词的逻辑补的包装器函数对象。
一元谓词必须定义成员类型 argument_type
,它可转换为谓词的形参类型。从 std::ref、std::cref、std::negate、std::logical_not、std::mem_fn、std::function、std::hash 或调用 std::not1 获得的一元函数对象已定义此类型,派生于已弃用的 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 解法: // 用 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 对象 (函数模板) |
(deprecated in C++11)(C++17 中移除) |
从函数指针创建与适配器兼容的函数对象包装器 (函数模板) |
(C++11 中弃用)(C++17 中移除) |
与适配器兼容的一元函数基类 (类模板) |