1#include <numeric> // Accumulate
2#include <vector> // Vector
3using namespace std;
4
5int main()
6{
7 vector<int> nums{1,2,3,4,5};
8 int sum = 0;
9 sum = accumulate(nums.begin(), nums.end(), sum);
10 // nums.begin() -> first number in a list
11 // nums.end() -> last number in a list
12 // sum -> starting value before accumulating: Here its 0
13}
1accumulate(first, last, sum);
2first, last : first and last elements of range
3 whose elements are to be added
4 suppose array is 'a'.
5sum : initial value of the sum
6Eg: int sum=0;
7 accumulate(a.begin(),a.end(),sum);