Johnny Pauling Johnny Pauling - 1 year ago 74
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download