The default operation is to add the elements up, but a different operation can be specified as binary_op.
The behavior of this function template is equivalent to:
| 1 2 3 4 5 6 7 |
|
Parameters
- first, last
- Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- init
- Initial value for the accumulator.
- binary_op
- Binary operation taking two elements of type T as argument, and returning the result of an accumulation operation. This can either be a pointer to a function or an object whose class overloads operator().
Return value
The result of accumulating init and all the elements in the range [first,last).Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Output:
using default accumulate: 160 using functional's minus: 40 using custom function: 220 using custom object: 280 |
See also
| inner_product | Compute cumulative inner product of range (function template) |
| partial_sum | Compute partial sums of range (function template) |
| adjacent_difference | Compute adjacent difference of range (function template) |
