C++ return the sum of the 2 smallest elements in a vector
Missing headers

This code needs to include &lt;vector&gt; (for std::vector&lt;int&gt;) and &lt;cstdint&gt; (for std::uint64_t).

Don't pass collections by value

The function signature int smallest(std::vector&lt;int&gt; numbers) makes a copy of the input vector.  If we follow the hint that we shouldn't modify it, we can pass by reference:

int smallest(const std::vector&lt;int&gt;&amp; numbers);


Even better would be to act like the standard algorithms, and not depend on the type of container at all:

template &lt;typename Iter&gt;
typename std::iterator_traits&lt;Iter&gt;::value_type
smallest(Iter first, Iter last);


Use the correct type for indexing

When indexing an collection, int isn't necessarily adequate.  Instead you should be using std::vector&lt;int&gt;::size_type (which is the same as std::size_t on many implementations):

using index_t = std::vector&lt;int&gt;::size_type;
for (index_t i = 0;  i &lt; numbers.size();  ++i)


Use the standard algorithms

#include &lt;algorithm&gt;


That gives you std::partial_sort_copy().  You can fill a two-element array with the lowest two elements:

using std::begin;
using std::end;
int first_two[2];

std::partial_sort_copy(begin(nums), end(nums),
                       begin(first_two), end(first_two));

return std::accumulate(begin(first_two), end(first_two), 0);


(Yes, std::accumulate() is overkill, but it does show how to generalize this).