There are some subtle differences. In the first case (std::vector<>::insert
) you are giving a range to the container, so it can calculate the distance and perform a single allocator to grow to the final required size. In the second case (std::copy
) that information is not directly present in the interface, and it could potentially cause multiple reallocations of the buffer.
Note that even if multiple reallocations are needed, the amortized cost of insertion must still be constant, so this does not imply an asymptotic cost change, but might matter. Also note that a particularly smart implementation of the library has all the required information to make the second version as efficient by specializing the behavior of std::copy
to handle back insert iterators specially (although I have not checked whether any implementation actually does this).