header file for accumulate 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: header file headers

#ifndef HEADER_FILE
#define HEADER_FILE

the entire header file file

#endif

Example 3: accumulate in cpp

accumulate(start, end, initial_sum);

Tags: