accumulate in vector c++ code example
Example 1: accumulate c++
#include <numeric> // Accumulate
#include <vector> // Vector
using namespace std;
int main()
{
vector<int> nums{1,2,3,4,5};
int sum = 0;
sum = accumulate(nums.begin(), nums.end(), sum);
// nums.begin() -> first number in a list
// nums.end() -> last number in a list
// sum -> starting value before accumulating: Here its 0
}
Example 2: accumulate in cpp
accumulate(start, end, initial_sum);