Search code examples
c++deque

How to get capacity of a deque?


So I recently covered deques and I was wondering, are there any ways to access the capacity() of a std::deque such as we do it on a std::vector?

I found this member function std::deque::max_size however this represents:

The maximum number of elements a deque container can hold as content.

and does not correspond to what I want.

Any ideas?


Solution

  • A double-ended queue (std::deque) doesn't have a capacity. It is because when adding new elements, it allocates more memory, unlike std::vector, which allocates some memory, and when it's full, it moves all elements to a new location.

    This means that the memory isn't contigous, unlike in std::vector. max_size is the theoretical limit on the number of elements, which is usually far bigger then your actual needs.