Source of partial_sum1a.cpp


  1: //partial_sum1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <numeric>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL partial_sum() "
 11:         "algorithm (default\nversion) from <numeric> to find the running "
 12:         "totals of integer values stored in\na vector of integers and "
 13:         "write those sums out to the same vector of integers.";
 14:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 16:     int a[] = {1, 2, 3, 4, 5};
 17:     vector<int> v(a, a+5);

 19:     cout << "\nHere are the initial values in the vector:\n";
 20:     for (vector<int>::size_type i=0; i<v.size(); i++)
 21:         cout << v.at(i) << " ";
 22:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 24:     vector<int>::iterator p = partial_sum(v.begin(), v.end(), v.begin());
 25:     cout << "\nAnd here is the same vector after the partial sums "
 26:         "\nhave been computed and the original values overwritten:\n";
 27:     for (vector<int>::size_type i=0; i<v.size(); i++)
 28:         cout << v.at(i) << " ";
 29:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 31:     cout << "\nThe iterator p returned by the algorithm points to ";
 32:     if (p == v.end())
 33:         cout << "\nthe end of the ouput container.";
 34:     else
 35:         cout << "the value " << *p << ".";
 36:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 37: }