Advent of Code 2017, Day 1: Sum of digits matching the next digit (circular list)
I think std::rotate can lead to a somewhat simpler approach:


get the input
create a copy
rotate the copy by one element
Accumulate the sum of elements that match between the two


Code might look something on this general order:

int sum_matching(std::vector&lt;int&gt; const &amp;digits) {
    std::vector&lt;int&gt; copy{digits};

    std::rotate(copy.begin(), copy.begin()+1, copy.end());

    int sum = 0;
    for (int i=0; i&lt;digits.size(); i++)
        if (digits[i] == copy[i])
            sum += digits[i];
    return sum;
}


I'm pretty sure you could hijack std::inner_product to do that final loop, but given its name, that would be misleading, so probably better avoided. On the other hand, if your compiler includes all the latest C++17 goodness, you could use std::transform_reduce though:

int main() {
    std::vector&lt;int&gt; inputs{1, 2, 3, 3, 1};

    std::vector&lt;int&gt; copy{inputs};
    std::rotate(copy.begin(), copy.begin()+1, copy.end());

    int result = std::transform_reduce(inputs.begin(), inputs.end(), 
                    copy.begin(), 
                    0,
                    [](int a, int b) { return a + b; },
                    [](int a, int b) { return a==b ? a : 0; });

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


If you'd like to play with that using a (strictly prototype) implementation of transform_reduce, it's not terribly complex--something on this order:

template&lt;class InputIterator1, 
         class InputIterator2, 
         class T, 
         class BinaryOperation1, 
         class BinaryOperation2&gt;
T transform_reduce(InputIterator1 first1, InputIterator1 last1,
                    InputIterator2 first2,
                    T init,
                    BinaryOperation1 binary_op1,
                    BinaryOperation2 binary_op2) 
{
    for ( ; first1 != last1; ++first1, ++first2) {
        init = binary_op1(init, binary_op2(*first1, *first2));
    }
    return init;
}


Note, however, that this is a purely serial implementation--part of the intent of transform_reduce is that it will support parallel implementation, which this doesn't (but that's mostly a restriction that binary_op1 and binary_op2 must be commutative and associative, not a requirement on the implementation). Note that the standard includes a number of overloads of std::transform_reduce--I've included an implementation of the one used in the preceding code.