Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::accumulate algorithm

Reference for C++ std::accumulate: fold a range into one value with + or a custom operation, the initial value type trap, and the C++23 ranges::fold_left.

std::accumulate (from <numeric>) folds a range into a single value. It starts with an initial value and, for each element from left to right, replaces the running result with result + element. With a custom binary operation you can compute products, concatenations, maximums or anything else that combines two values.

The type of the initial value decides the type of the result and of every intermediate step. That makes accumulate(v.begin(), v.end(), 0) on a vector of doubles silently truncate to int, the most famous mistake with this function.

C++
int total = std::accumulate(v.begin(), v.end(), 0);
double avg = std::accumulate(v.begin(), v.end(), 0.0) / v.size();

Try it

C++ Sums, products and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ The initial-value type trap

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class InIt, class T>
T accumulate(InIt first, InIt last, T init);

template<class InIt, class T, class BinaryOp>
T accumulate(InIt first, InIt last, T init, BinaryOp op);
Name Description
first, last The range to fold.
init Starting value; its type T is the type of the accumulator and of the result.
op Called as op(acc, element); its result replaces acc. Defaults to +.

Returns: the final accumulator, of type T; init if the range is empty.

Errors / undefined behavior: none inherent; signed overflow in acc + x is undefined behavior. op must not modify elements or invalidate iterators.

Usage notes

  • Initial value type matters. Use 0.0 for doubles, 0LL for large integer sums, std::string{} for strings. A literal "" as init does not compile with a lambda that expects a std::string.
  • Strict left-to-right order, one element at a time. Because the order is guaranteed, non-associative operations (subtraction, string joining) are well defined. For associative operations that can be reordered, std::reduce is allowed to be faster and parallel.
  • No parallel overload and no execution policy: accumulate is inherently sequential.
  • Floating point: results can differ slightly from reduce because addition is not associative for floats.
  • Avoid copying large accumulators: take the accumulator by value and std::move it into the result, as in the string example (C++20 specifies that accumulate moves acc into op).
  • Lambda parameters: if the element type differs from the accumulator type the lambda needs two different parameter types.
  • C++23: std::ranges::fold_left, fold_left_first, fold_right and others fold a whole range and deduce the result type from the operation. See also constrained algorithms.
  • Complexity: exactly last - first applications of op.