1: //accumulate1a.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 accumulate() "
11: "algorithm (default\nversion) from <numeric> to find the sum of "
12: "integer values stored in a vector.";
13: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
15: vector<int> v{2, 4, 6, 8, 10}; //since C++11
16: cout << "\nHere are the integer values in the vector:\n";
17: for (int i : v) cout << i << " "; //since C++11
18: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
20: cout << "\nThe sum of these values is "
21: << accumulate(v.begin(), v.end(), 0) << ".";
22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
24: cout << "\nThe sum of the middle three values and an intial "
25: "value of -5 is "
26: << accumulate(v.begin()+1, v.end()-1, -5) << ".";
27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
29: cout << "\nThe sum of the values in an empty range, with "
30: "an intial value of 22, is "
31: << accumulate(v.begin(), v.begin(), 22) << ".";
32: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
33: }