I would just use adjacent_difference instd::adjacent_difference
from the numeric<numeric>
library. It's designed to accept custom binary ops such as plus<>plus<>
and multiplies<>multiplies<>
from the functional<functional>
library.
std::adjacent_difference(inputs.begin(), inputs.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>());
If you are going to use your own adjacent sum, please include handling for empty/single element lists.
template <class InIt, class OutIt>
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) {
if (begin == end) // Corbin
return result;
auto last = *begin; // Loki
for (*result = last; ++begin != end;) { // Me
auto next = *begin;
*++result = last + next;
last = std::move(next);
}
return result;
}