The Wayback Machine - https://web.archive.org/web/20250505173429/https://www.geeksforgeeks.org/stdmultiplies-in-cpp/
Open In App

std::multiplies in C++

Last Updated : 02 Aug, 2017
Comments
Improve
Suggest changes
Like Article
Like
Report

Function object for performing multiplication. Effectively calls operator* on two instances of type T.

Syntax :

template  struct multiplies : binary_function  
{
    T operator() (const T& x, const T& y) const {return x*y;}
};

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

Member types :
x : Type of the first argument in member operator()
y : Type of the second argument in member operator()
result_type : Type returned by member operator()

Example :





Output:

10 40 90 160 250 

Another Example :




// C++ program to illustrate std::multiplies
// by multiplying all array elements with a number
#include <bits/stdc++.h>
  
int main()
{
    // Array with elements to be multiplying
    int arr[] = { 10, 20, 30 };
  
    // size of array
    int size = sizeof(arr) / sizeof(arr[0]);
  
    // Variable with which array is to be multiplied
    int num = 10;
  
    // Variable to store result
    int result;
  
    // using std::accumulate to perform multiplication on array with num
    // using std::multiplies
    result = std::accumulate(arr, arr + size, num, std::multiplies<int>());
  
    // Printing the result
    std::cout << "The result of 10 * 10 * 20 * 30 is " << result;
  
    return 0;
}


Output:

60000


Next Article
Article Tags :
Practice Tags :

Similar Reads