function template
std::not1
<functional>
template <class Predicate>
unary_negate<Predicate> not1 (const Predicate& pred);
Return negation of unary function object
This function constructs a function object that has the opposite behavior of
pred (its argument, another function object).
Function objects are objects whose class defines member function
operator(). This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.
The function object returned by
not1 has its
operator() defined such that it returns
true when
pred would return
false, and
false when
pred would return
true.
It is defined with the same behavior as:
1 2 3 4 5
|
template <class Predicate>
unary_negate<Predicate> not1 (const Predicate& pred)
{
return unary_negate<Predicate>(pred);
}
|
not1 is specifically designed to negate function objects (
predicates) derived from
unary_function (an
argument_type member is required). For binary function objects, see
not2.
Parameters
- Predicate
- Unary function object derived from unary_function.
Return value
An unary function object with the opposite behavior of
pred.
unary_negate is a type derived from
unary_function.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// not1 example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
struct IsOdd : unary_function<int,bool> {
bool operator() (const int& x) const {return x%2==1;}
};
int main () {
int values[] = {1,2,3,4,5};
int cx;
cx = count_if ( values, values+5, not1(IsOdd()) );
cout << "There are " << cx << " elements with even values.\n";
return 0;
}
|
Output:
There are 2 elements with even values.
|
See also
- not2
- Return negation of binary function object (function template)
- unary_negate
- Generate negation of unary function object class (class template)
- unary_function
- Unary function object base class (class template)