Johnny Pauling Johnny Pauling - 2 months ago 8
C++ Question

Deque - how come "reserve" doesn't exist?

The standard STL vector container has a "reserve" function to reserve uninitialized memory that can be used later to prevent reallocations.

How come that the other deque container hasn't it?

Answer

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 frequent std::vector resizing can be costly that vector::reserve() exists.

Conversely, a deque can always add more memory without needing to relocate the existing elements.