class template
<functional>

std::multiplies

template <class T> struct multiplies;
Multiplication function object class
Binary function object class whose call returns the result of multiplying its two arguments (as returned by operator *).

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 function call.

It is defined with the same behavior as:

1
2
3
template <class T> struct multiplies : binary_function <T,T,T> {
  T operator() (const T& x, const T& y) const {return x*y;}
};

1
2
3
4
5
6
template <class T> struct multiplies {
  T operator() (const T& x, const T& y) const {return x*y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};


Objects of this class can be used on standard algorithms such as transform or accumulate.

Template parameters

T
Type of the arguments and return type of the functional call.
The type shall support the operation (operator*).

Member types

member typedefinitionnotes
first_argument_typeTType of the first argument in member operator()
second_argument_typeTType of the second argument in member operator()
result_typeTType returned by member operator()

Member functions

T operator() (const T& x, const T& y)
Member function returning the multiplication of its two arguments (x*y).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// factorials (multiplies example)
#include <iostream>     // std::cout
#include <functional>   // std::multiplies
#include <numeric>      // std::partial_sum

int main () {
  int numbers[9];
  int factorials[9];
  for (int i=0;i<9;i++) numbers[i]=i+1;
  std::partial_sum (numbers, numbers+9, factorials, std::multiplies<int>());
  for (int i=0; i<9; i++)
    std::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