CPP   77
accumulate
Guest on 3rd February 2023 01:41:09 AM


  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <numeric>  // for accumulate
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.   cout << "Demonstrating the accumulate function." << endl;
  10.   int x[5] = {2, 3, 5, 7, 11};
  11.   // Initialize vector1 to x[0] through x[4]:
  12.   vector<int> vector1(&x[0], &x[5]);
  13.  
  14.   int sum = accumulate(vector1.begin(), vector1.end(), 0);
  15.    
  16.   assert (sum == 28);
  17.   cout << " --- Ok." << endl;
  18.   return 0;
  19. }

Raw Paste

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