1 vector<int> v{1,2,3,4,5,6,7,8,9};
2 int sum = 0;
3//Method 1:
4 sum = accumulate(v.begin(), v.end(), 0);
5//Method 2:
6 for(auto& i : v) sum+=i;
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
1#include <numeric>
2
3sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);
4