class template
std::unary_negate
<functional>
template <class Predicate> class unary_negate;
Generate negation of unary function object class
Generates a function object class that negates the behavior of an unary function object class.
unary_negate is generally used as a type. The function
not1 (also defined in header
<functional>) can be used to directly construct an object of this type.
unary_negate is constructed using an
unary function object as argument. A copy of this object is used by its member
operator() to return
true whenever the original object would return
false, and
false whenever the object would return
true, inverting its behavior.
This class is derived from
unary_function and is defined as:
1 2 3 4 5 6 7 8 9 10
|
template <class Predicate> class unary_negate
: public unary_function <typename Predicate::argument_type,bool>
{
protected:
Predicate fn;
public:
explicit unary_negate ( const Predicate& pred ) : fn (pred) {}
bool operator() (const typename Predicate::argument_type& x) const
{ return !fn(x); }
};
|
unary_negate class is specifically designed to negate function objects (
predicates) derived from
unary_function (it requires an
argument_type member).
Members
- constructor
- Constructs an object with the opposite behavior than the object passed as its argument.
- operator()
- Member function returning the opposite of the function object with which the object was constructed.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// unary_negate example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
struct IsOdd_class : unary_function<int,bool> {
bool operator() (const int& x) const {return x%2==1;}
} IsOdd_object;
int main () {
unary_negate<IsOdd_class> IsEven_object (IsOdd_object);
int values[] = {1,2,3,4,5};
int cx;
cx = count_if ( values, values+5, IsEven_object );
cout << "There are " << cx << " elements with even values.\n";
return 0;
}
|
Output:
There are 2 elements with even values.
|
See also
- not1
- Return negation of unary function object (function template)
- binary_negate
- Generate negation of binary function object class (class template)
- unary_function
- Unary function object base class (class template)