TL;DR version:
Full explanation:
A std::vector
has two useful metrics:
size
, which is the number of elements stored.capacity
, which is the number of elements it's currently capable of storing.
capacity >= size
at all times.
The capacity
is the length of the internal dynamically-allocated array.* When you insert an element, the size
increments by 1. But once it reaches capacity
, a new, larger array must be allocated (hence increasing the capacity
). This requires all the elements to be copied across, and the originals to be deleted. So all their addresses change.
* This is the typical internal implementation of a
std::vector
.