class template
std::minus
<functional>
template <class T> struct minus;
Subtraction function object class
This class defines function objects for the subtraction 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.
minus has its
operator() member defined such that it returns the result of subtracting its second argument from its first argument.
This class is derived from
binary_function and is defined as:
1 2 3 4
|
template <class T> struct minus : binary_function <T,T,T> {
T operator() (const T& x, const T& y) const
{return x-y;}
};
|
Objects of this class can be used with some standard algorithms such as
transform or
accumulate.
Members
- T operator() (const T& x, const T& y)
- Member function returning x-y.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13
|
// minus example
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int main () {
int numbers[]={10,20,30};
int result;
result = accumulate (numbers, numbers+3, 100, minus<int>() );
cout << "The result of 100-10-20-30 is " << result << ".\n";
return 0;
}
|
Output:
The result of 100-10-20-30 is 40.
|
See also
- plus
- Addition function object class (class template)
- multiplies
- Multiplication function object class (class template)
- divides
- Division function object class (class template)
- modulus
- Modulus function object class (class template
)
- negate
- Negative function object class (class template)
- equal_to
- Function object class for equality comparison (class template
)
- binary_function
- Binary function object base class (class template)