accumulate vector c++ code example
Example 1: accumulate c++
#include <numeric>
#include <vector>
using namespace std;
int main()
{
vector<int> nums{1,2,3,4,5};
int sum = 0;
sum = accumulate(nums.begin(), nums.end(), sum);
}
Example 2: accumulate in cpp
accumulate(start, end, initial_sum);
Example 3: accumulate vector c++
accumulate(first, last, sum);
accumulate(first, last, sum, myfun);
first, last : first and last elements of range
whose elements are to be added
sum : initial value of the sum
myfun : a function for performing any
specific task. For example, we can
find product of elements between
first and last.
int a[] = {5 , 10 , 15} ;
int res = accumulate(a,a+3,0);