What happens to a pointer that points to an element in a vector when I shuffle it?

The pointer will continue to point to the same location, so when you shuffle, it'll point to whatever element has been moved into the location you specified.

When you expand the size of a vector, all existing pointers and iterators into the vector can become invalid. When you shuffle, they continue to refer to the same location, which will (usually) contain a different value than it did before shuffling.

Reducing the size of a vector will depend on exactly how you do that. One way is to create a temporary vector as a copy of the current vector, swap the two, then destroy the temporary (usually implicitly, by letting it go out of scope). If you do this, the pointers will be into the temporary, and be invalidated when it's destroyed.

If you use shrink_to_fit that (probably) won't invalidate iterators/pointers, but may not have any effect (the standard specifies that it's a non-binding request, and doesn't say anything about it invalidating iterators/pointers).


If the vector is shuffled without being resized then the pointer still points to the same location , which will probably contain a different element.

If the vector is resized to be larger, then the pointer is said to be "invalidated" and it has the same status as an uninitialized pointer, i.e. evaluating it or trying to read through it causes undefined behaviour.


If I now shuffle the vector, will it still point to the same element (the third) or will it point the the new location where the used-to-third element has moved?

Shuffling elements is just a matter of copying/swapping elements through the various "buckets" in the array, while your pointer just points to "that fixed position in memory". So, it'll keep pointing to whatever stays in third position in the array.

Then I like to make the question a little bit more general: How do pointer and iterators to elements in a vector behave when the vector is expanded, reduced or shuffled?

Expand: all iterators/references/pointers may be invalidated.

Reduced: as far as they point to elements before those removed, they are kept valid unless you do a shrink_to_fit. Iterators/pointers to elements you removed are obviously invalid.

Shuffled: you are moving around stuff without causing reallocations, so iterators and references are still valid.

Notice that all this stuff is typically reported in most C++ documentation sources.


The conceptual rule to remember for vectors is that they are just a box around a dynamic array, and iterators and pointers to elements are conceptually the same thing (actually, std::vector<T>::iterator could be a typedef for T *). The same holds for references (which are pointers in disguise).

If an operation may need reallocate the array (=the array needs to grow, or you explicitly requested it to shrink), then all iterators/pointers/references are going to be invalidated. If you remove elements, then pointers pointing past the "conceptual end" of the vector will point to invalid elements. If the size stays the same, no reallocation needs to occur.