// 默认为累加和
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init);
// 自定义op,更改accumulate的操作
template <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init, BinaryOperation binary_op);
2. accumulate的底层实现
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init)
{
while (first!=last) {
init = init + *first; // or: init=binary_op(init,*first) for the binary_op version
++first;
}
return init;
}