CPP   86
accumulate vector1
Guest on 3rd February 2023 01:42:04 AM


  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <numeric>  // For accumulate
  5. using namespace std;
  6.  
  7. int mult(int x, int y) { return x * y; }
  8.  
  9. int main()
  10. {
  11.   cout << "Using generic accumulate algorithm to "
  12.        << "compute a product." << endl;
  13.  
  14.   int x[5] = {2, 3, 5, 7, 11};
  15.  
  16.   // Initialize vector1 to x[0] through x[4]:
  17.   vector<int> vector1(&x[0], &x[5]);
  18.  
  19.   int product = accumulate(vector1.begin(), vector1.end(),
  20.                            1, mult);
  21.    
  22.   assert (product == 2310);
  23.   cout << " --- Ok." << endl;
  24.   return 0;
  25. }

Raw Paste

Login or Register to edit or fork this paste. It's free.