Here you are already well explained how . Let me say a few words about why .
The fact is that the increase in capacity is an expensive operation. At the core of std::vector is an ordinary array, which in principle cannot be expanded. * So, capacity just shows the size of this array.
How does the capacity increase? Very simple: a new array is allocated, all elements of the current one are copied there, the old array is destroyed. Yes, this is a rather expensive operation, it can cause copying / moving constructors of array elements **. Therefore, it is advisable to do it as rarely as possible, its cost is O(n) , where n is the current length of the array.
Suppose we do not know the size of the necessary data in advance, and put it into std::vector dynamically, using push_back . If at each step we make a copy, when we reach the size N we have to do 1 copy at the 2nd step, 2 at the 3rd, 3 at the 4th, ..., N-1 at the N . The total number of copies is N*(N-1)/2 = O(N^2) . For an array size of 1000 items, this is about a million copies!
Therefore, strategies were invented to reduce the number of copies. A popular strategy is to increase the capacity in powers of two. Let's look at the asymptotics for this. Let the final size of the array N , k be a power of two such that 2^k <= N < 2^(k+1) . We are copying now not every time, but less and less: after the 1st step (1 element), after the second (2 elements), after the 4th (4 elements), after the 8th (8 elements) and etc. Total 1 + 2 + 4 + ... + 2^k = 2^(k+1) - 1 < 2*N copies. Thus, the number of copies remains O(N) (due to less optimal memory consumption).
* No, realloc also not an option, the memory is copied into a new piece in the same way, it is just hidden from us. Plus constructors / destructors are not executed.
** If the class has a relocating constructor declared as noexcept , then it will be called, otherwise copying. Thanks @gbg for the hint.