在线文档教程
C++
应用 | Utilities

std::unary_negate

性病:一元[医]否定

Defined in header
template< class Predicate > struct unary_negate : public std::unary_function<Predicate::argument_type, bool>;(until C++11)
template< class Predicate > struct unary_negate;(since C++11) (deprecated in C++17)

unary_negate是一个包装函数对象,返回它所持有的一元谓词的补码。

一元谓词类型必须定义成员类型,argument_type,它可转换为谓词%27s参数类型。获取的一元函数对象。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...

成员类型

TypeDefinition
argument_typePredicate::argument_type
result_typebool

成员函数

(constructor)constructs a new unary_negate object with the supplied predicate (public member function)
operator()returns the logical complement of the result of a call to the stored predicate (public member function)

性病:一元[医]否定::一元[医]否定

explicit unary_negate( Predicate const& pred (until C++14)
explicit constexpr unary_negate( Predicate const& pred (since C++14)

构造一个unary_negate具有存储谓词的函数对象。pred...

参数

pred-predicate function object

性病:一元[医]否定::操作员%28%29

bool operator()( argument_type const& x ) const;(until C++14)
constexpr bool operator()( argument_type const& x ) const;(since C++14)

返回调用结果的逻辑补码。pred(x)...

参数

x-argument to pass through to predicate

返回值

调用结果的逻辑补充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 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 */ }

二次

产出:

二次

3

二次

另见

binary_negate (deprecated)wrapper function object returning the complement of the binary predicate it holds (class template)
function (C++11)wraps callable object of any type with specified function call signature (class template)
not1 (deprecated)constructs custom std::unary_negate object (function template)
ptr_fun (until C++17)creates an adaptor-compatible function object wrapper from a pointer to function (function template)
unary_function (until C++17)adaptor-compatible unary function base class (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/Functional/Monary[医]否定