Using the generic accumulate algorithm to compute a product by using a function object : multiply « STL Algorithms Helper « C++ Tutorial






#include <iostream>
#include <vector>
#include <cassert>
#include <numeric>  // For accumulate
using namespace std; 

class multiply {
 public:
  int operator()(int x, int y) const { return x * y; }
};

int main()
{
  int x[5] = {2, 3, 5, 7, 11};

  vector<int> vector1(&x[0], &x[5]); 
  
  int product = accumulate(vector1.begin(), vector1.end(),
                           1, multiply());
    

  cout << product << endl;
  return 0;
}
2310








32.12.multiply
32.12.1.Using the generic accumulate algorithm to compute a product by using a function object
32.12.2.Using the generic accumulate algorithm to compute a product by using multiplies