| multiplies |
class template |
template <class T> struct multiplies; |
<functional> |
Multiplication function object class
This class defines function objects for the multiplication 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.
multiplies has its operator() member defined such that it returns the product of its two arguments.
This class is derived from binary_function and is defined as:
template <class T> struct multiplies : 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 the product of its two arguments (x*y).
Example
// factorials (multiplies example)
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int main () {
int numbers[9];
int factorials[9];
for (int i=0;i<9;i++) numbers[i]=i+1;
partial_sum ( numbers, numbers+9, factorials, multiplies<int>() );
for (int i=0; i<9; i++)
cout << numbers[i] << "! is " << factorials[i] << "\n";
return 0;
} |
Output:
1! is 1 2! is 2 3! is 6 4! is 24 5! is 120 6! is 720 7! is 5040 8! is 40320 9! is 362880
|
See also
| plus | Addition function object class (class template) |
| minus | Subtraction function object class (class template) |
| divides | Division function object class (class template) |
| modulus | Mudulus function object class (class template) |
| negate | Negative function object class (class template) |
| equal_to | Function object class for equality comparison (class template) |