Going through and inserting data to std vectors

Solution 1:

calling insert() is going to invalidate your iterators.

From http://en.cppreference.com/w/cpp/container/vector/insert:

Causes reallocation if the new size() is greater than the old capacity(). If the new size() is greater than capacity(), all iterators and references are invalidated. Otherwise, only the iterators and references before the insertion point remain valid. The past-the-end iterator is also invalidated.

The insert() function returns a iterator to the inserted element. Since this is a valid iterator you can capture it and use it for the next iteration. Here is a little example of how that works:

std::vector<int> data = { 1, 2, 3, 4, 5 };
auto it = data.begin();
std::advance(it, 3);
for (int i = 0; i < 3; i++)
{
    it = data.insert(it, 9);
    ++it;
}

data will contain { 1, 2, 3, 9, 9, 9, 4, 5 } at the end of the for loop.