std::unary_negate
Min standard notice:
Header: <functional>
std::unary_negate is a wrapper function object returning the complement of the unary predicate it holds.
# Declarations
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) (removed in C++20)
# Parameters
pred: predicate function object
# Return value
The logical complement of the result of calling pred(x).
# Example
#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);
}