STL container adapter questions

stack
queue
priority_queue

when the empty() yields true, is the memory really zero?
i didn't find capacity() for these containers, so how to release the memory for the above three containers?
for other containers, they all have swap() which can do the trick.
what about the above three?
thanks.
Probably not. In the case of vector no. Stack and priority_queue uses the vector by default and the vector's capacity can only be trimmed using the swap trick. Since you don't have access to the underlying container you could not destroy the memory within the vector unless the stack object is destroyed. queue uses deque by default which has no capacity function therefore I am not sure what it does with its underlying memory when elements are erased.

You can provide user defined containers or boost containers as the template arg and override the default if you need finer control over this. However it is probably not a good idea to destroy the memory when you pop items anyway. The containers were designed to hold onto their memory for a reason. THis is so that erasing and inserting data is more efficient. You don't want to have to keep deleting and reallocating memory as you push and pop right?

These adapters are created for more specific purposes where you wouldn't really be concerned so much with those details. If you really needed to know the capacity then you would just use the vector directly. In all honesty I have never used any of those adapters.
Thank you very much.
because sometimes, i use queue for breadth first search. Actually, i'm not eager about releasing the memory. i just curious about this topic.
thanks!
Last edited on
Topic archived. No new replies allowed.