accumulate function template — Computes a value from all items in a range
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.
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.