'c++ priority-queue ,pop() element, but memory not release

I use priority-queue from c++ stl library.but when I pop some elements, the memory space of queue not be less.i want memory space will be less immediately when elements are poped.for example, my queue have five elements, when I pop one element.i want queue only use four elements memory space.



Solution 1:[1]

std::priority_queue is not a container. It is a container adaptor. A container adaptor wraps another container. By default, std::priority_queue wraps std::vector. std::vector does not release the storage when you pop an element.

There is no standard container that satisfies the requirements of being wrapped by std::priority_queue that also releases storage immediately when element is popped. std::deque is the other valid choice besides std::vector and it practically releases memory every Nth element where N is implementation specific.

In order to achieve your goal, you can implement a custom SequenceContainer that provides RandomAccessIterator and that behaves in the way that you want (at the cost of the overhead that you must pay to achieve that behaviour). You can then adopt your custom container with std::priority_queue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1