Name
accumulate function template — Computes a value from all items in a range
Synopsis
template <typename InputIter, typename T> T accumulate(InputIter first, InputIter last, T init); template < typename InputIter, typename T, typename BinaryOp> T accumulate(InputIter first, InputIter last, T init, BinaryOp binary_op);
The accumulate
function
template sums all the values in the range [first
, last
) added with init
and returns the result. The result
and intermediate sum have the same type as init
. The second version calls binary_op
instead of using the addition
(+
) operator.
Technical Notes
The result is computed as follows: for each i in the range [first, last), tmp = binary_op
(tmp, *i), in which tmp is initialized to init. The final value of tmp is returned.
The binary_op
function or
functor must not have any side effects.
Complexity is linear: binary_op
is called exactly last - first times.
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.