std::accumulate() computes the sum of the given elements in the range. std::partial_sum() computes the partial sums of the elements in the subranges of the range.

Syntax

Below is the syntax of accumulate.

template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init);

template <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
  • first, last : Iterators to the initial and final positions in a sequence. Range used is [first,last), which contains all the elements between first and last, including the element pointed by first.
  • init : Initial value for the accumulator.
  • binary_op : Binary operation taking an element of type T as first argument and an element in the range as second. First argument is current accumulation value (initialized to init) and second argument is the current element. It returns a value that can be assigned to type T.

Similarly the syntax of partial_sum() is

template <class InputIterator, class OutputIterator>
OutputIterator partial_sum (InputIterator first, InputIterator last, OutputIterator result);

template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum (InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op);
  • first, last : Iterators to the initial and final positions in a sequence.
  • result : Iterator to the initial position in the destination sequence where the partial sums are stored. Destination sequence should have a size large enough to contain as many elements as the range above.
  • binary_op : Binary operation taking two elements of the type pointed by the InputIterator as arguments. It return the result of the replacement for the sum operation.

Example

The following code uses accumulate() and partial_sum() to find the sum of contents of vector. Default behaviour of accumulate() is sum all the element in sequence. To change the default behaviour, function pointer can be passed to accumulate(), as shown in below example. First argument passed to the function pointer is cumulative sum prior to previous index.

partial_sum() computes the partial sums of the elements in the subranges of the range and writes them to the output sequence.

#include <iostream>     // std::cout
#include <functional>   // std::minus, std::multiplies
#include <numeric>      // std::accumulate, std::partial_sum

int myfunction (int x, int y) {return x+2*y;}

struct myclass {
	int operator()(int x, int y) {return x+2*y;}
} myobject;

int main () {

  int init = 0;
  int numbers[] = {10,20,30};
  int result[3];

  // Using default accumulate
  std::cout << std::accumulate(numbers,numbers+3,init);
  std::cout << '\n';
  // Output : 60

  // Using custom function
  std::cout << std::accumulate (numbers, numbers+3, init, myfunction);
  std::cout << '\n';
  // Output : 120 
  // Explanation - 0: 0 + 10 * 2, 1: 20 + 20 *2, 2: 60 + 30 * 2 

  // Using custom object
  std::cout << std::accumulate (numbers, numbers+3, init, myobject);
  std::cout << '\n';
  // Output : 120 

  // Using default partial_sum
  std::partial_sum (numbers, numbers+3, result);
  for (int i=0; i < 3; i++) 
    std::cout << result[i] << ' ';

  // Output : 10 30 60 
  std::cout << '\n';

  // Using custom function
  std::partial_sum (numbers, numbers+3, result, myfunction);
  for (int i=0; i<3; i++)
    std::cout << result[i] << ' ';
  // Output : 10 50 110
  // Explanation - 0: 10, 1 : 10 + 2 * 20, 2 : 50 + 2 * 30

  std::cout << '\n';

  return 0;
}