Place integers into a vector, sum each adjacent pair, refill vector with only the sums of each pair i.e remove all the original data from the vector Is there a quicker/better way of doing this, mine works great for small vectors but if i get a vector with an arbitrarily large size this would probably take forever to run. Thanks.(P.S. its been a while since I've written any code so please excuse me for any sloppiness).

/*Fill a vector with integers,
sum each pair of adjacent elements, 
and remove the pair of elements and place sum in the vector*/

#include &lt;iostream&gt;
#include &lt;vector&gt;


int main() {
    std::vector&lt;int&gt; numbers;
    std::cout &lt;&lt; "Please enter a list of integers:\n";

    int num1;
    while (std::cin &gt;&gt; num1)
        numbers.push_back(num1);

    // starting from back of the container with integer k representing last element
    // i representing the second to last
    for (int i = numbers.size() - 2, k = numbers.size() - 1; i &gt; -1; i-=2, k-=2) {
        numbers[i] += numbers[k];
        numbers.erase(numbers.begin() + k);
    }
}
It seems to me that a tiny bit of calculation can simplify the code quite a bit:

auto dest = numbers.size() % 2; // 0 if even, 1 if odd

for (auto src = dest; src &lt; numbers.size(); src += 2, ++dest)
    numbers[dest] = numbers[src] + numbers[src + 1];

numbers.resize(dest);


Although it has the same big-O complexity (linear in both cases) I'd expect this to typically be at least a little faster than the solution @larkey posted, for a couple of reasons. First, it avoids conditional execution by simply computing the starting source/destination positions. Second, when it resizes the array down to the correct size after the additions, it does so in one fell swoop, so to speak, instead of doing a pop_back individually on each item being removed.

If the vector contained objects with non-trivial destructors, the destructor for each object being removed would have to be invoked, so the resize would be linear on the number of items being removed. Given that these are ints, there's really no dtor to run, so the resize can be done with constant complexity (and I'd expect most current implementations of std::vector to include the specializations necessary to actually achieve constant complexity too).

More importantly (at least in my opinion), this seems to me to simplify the code so it's considerably easier to read and understand the intent and result.