Code for sums in a vector I posted this code in an answer earlier today, but thought it was worth seeing if somebody could suggest further improvements:

#include &lt;numeric&gt;
#include &lt;vector&gt;
#include &lt;iterator&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

template &lt;class InIt, class OutIt&gt;
OutIt adjacent_sum(InIt begin, InIt end, OutIt result) { 
    ++begin;     
    while (begin != end) {
        *result = *begin + *(begin-1);
        ++result;
        ++begin;
    }
    return result;
}

int main() { 
    std::vector&lt;int&gt; inputs { 1, 2, 3, 4, 5, 10, 9, 8, 7, 6};

    adjacent_sum(inputs.begin(), inputs.end(), 
        std::ostream_iterator&lt;int&gt;(std::cout, "\t"));

    std::cout &lt;&lt; "\n";

    std::transform(inputs.begin(), inputs.begin()+inputs.size()/2, 
        inputs.rbegin(), 
        std::ostream_iterator&lt;int&gt;(std::cout, "\t"), 
        std::plus&lt;int&gt;());
}


Here I've placed fixed input into the vector inputs. The intent is to first produce the sums of adjacent items in the input, and second produce the sum of the first item added to the last item, second item added to the second to last, and so on.
You make the assumption of random accesses iterators.
It would be nice to be able to do this with any iterator (including input iterator)

template &lt;class InIt, class OutIt&gt;
OutIt adjacent_sum(InIt begin, InIt end, OutIt result)
{ 
    if (begin == end)
    {     return result;    // From @Corbin
    }

    auto  last = (*begin);  // Get the value now before incrementing the iterator.
    ++begin;     
    while (begin != end)
    {
        auto next = (*begin);
        *result   = next + last;
        last      = next;

        ++result;
        ++begin;
    }
    return result;
}


Now the following works:

adjacent_sum(std::istream_iterator&lt;int&gt;(std::cin), std::istream_iterator&lt;int&gt;(), 
             std::ostream_iterator&lt;int&gt;(std::cout, "\t"));