What happens when you add elements to a data structure such as a vector while iterating over it. Can I not do this? Other containers have various rules about when iterators are and are not invalidated. This is a good post with details. This wiki entry also summarises them quite clearly. What happens when you add elements to a data structure such as a vector while iterating over it. Can I not to this? The iterator would become invalid IF the vector resizes itself. So you’re safe as long as the vector doesn’t resize itself. Initially the vector has some capacity (which you can know by calling vector::capacity().), and you add elements to it, and when it becomes full, it allocates larger size of memory, copying the elements from the old memory to the newly allocated memory, and then deletes the old memory, and the problem is that iterator still points to the old memory, which has been deallocated. That is how resizing invalidates iterator. Note: capacity() tells the maximum number of elements the vector can contain without allocating new memory, and size() tells the number of elements the vector currently containing. You could think about the case where your vector would need to be resized after a push_back. It would then need to be moved to a bigger memory spot and your iterators would now be invalid. It’s a bad idea in general, because if the vector is resized, the iterator will become invalid (it’s wrapping a pointer into the vector’s memory). It’s also not clear what your code is really trying to do. If the iterator somehow didn’t become invalid (suppose it was implemented as an index), I’d expect you to have an infinite loop there – the end would never be reached because you’re always adding elements. Assuming you want to loop over the original elements, and add one for each, one solution would be to add the new elements to a second vector, and then concatenate that at the end: Source.