25 Algorithms library [algorithms]

25.9 Generalized numeric operations [numeric.ops]

25.9.2 Accumulate [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);
Requires: T shall satisfy the Cpp17CopyConstructible ([tab:copyconstructible]Table *tab:copyconstructible) and Cpp17CopyAssignable ([tab:copyassignable]Table *tab:copyassignable) requirements.
In the range [first, last], binary_­op shall neither modify elements nor invalidate iterators or subranges.239
Effects: Computes its result by initializing the accumulator acc with the initial value init and then modifies it with acc = std::move(acc) + *i or acc = binary_­op(std::move(acc), *i) for every iterator i in the range [first, last) in order.240
The use of fully closed ranges is intentional.
accumulate is similar to the APL reduction operator and Common Lisp reduce function, but it avoids the difficulty of defining the result of reduction on an empty sequence by always requiring an initial value.