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}
1//Syntax
2accumulate(first, last, sum);
3accumulate(first, last, sum, myfun);
4
5first, last : first and last elements of range
6 whose elements are to be added
7sum : initial value of the sum
8myfun : a function for performing any
9 specific task. For example, we can
10 find product of elements between
11 first and last.
12//Example
13 int a[] = {5 , 10 , 15} ;
14 int res = accumulate(a,a+3,0); // 30