Increasing the size of a std::vector
can be costly. When a vector
outgrows its reserved space, the entire contents of the vector must be copied (or moved) to a larger reserve.
It is specifically because std::vector resizing can be costly that vector::reserve()
exists. reserve()
can prepare a std::vector
to anticipate reaching a certain size without exceeding its capacity.
Conversely, a deque
can always add more memory without needing to relocate the existing elements. If a std::deque
could reserve()
memory, there would be little to no noticeable benefit.