template <class T> struct negate; |
<functional> |
Negative function object class
This class defines function objects for the negative arithmetic operation.
Generically, function objects are instances of a class with member function operator() defined. 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.
negate has its operator() member defined such that it returns the same value with the opposite sign (i.e., its negation).
This class is derived from unary_function and is defined as:
template <class T> struct negate : unary_function <T,T> {
T operator() (const T& x) const
{return -x;}
}; |
Objects of this class can be used with some standard algorithms such as transform.
Members
- T operator() (const T& x)
- Member function returning -x.
Example
// negate example
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main () {
int numbers[]={10,-20,30,-40,50};
transform ( numbers, numbers+5, numbers, negate<int>() );
for (int i=0; i<5; i++)
cout << numbers[i] << " ";
return 0;
} |
Output:
See also
| plus | Addition function object class (class template) |
| minus | Subtraction function object class (class template) |
| multiplies | Multiplication function object class (class template) |
| divides | Division function object class (class template) |
| modulus | Mudulus function object class (class template) |
| equal_to | Function object class for equality comparison (class template) |