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 中移除)  | 
|
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 的函数对象亦然。
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 前) | |
|   explicit constexpr unary_negate( Predicate const& pred );  | 
(C++14 起) | |
以存储的谓词 pred 构造 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; for (int i = 0; i < 10; ++i) v.push_back(i); std::unary_negate<less_than_7> not_less_than_7((less_than_7())); std::cout << std::count_if(v.begin(), v.end(), not_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); */ }
输出:
3
参阅
|    (C++17 中弃用)(C++20 中移除)  | 
   包装器函数对象,返回所持有的二元谓词的补  (类模板)  | 
|    (C++11)  | 
  包装具有指定函数调用签名的任意类型的可调用对象  (类模板)  | 
|    (C++17 中弃用)(C++20 中移除)  | 
  构造定制的 std::unary_negate 对象  (函数模板)  | 
|    (C++11 中弃用)(C++17 中移除)  | 
   从函数指针创建与适配器兼容的函数对象包装器   (函数模板)  | 
|    (C++11 中弃用)(C++17 中移除)  | 
  与适配器兼容的一元函数基类  (类模板)  |