With the stl priority_queue you can set the underlying container, such as a vector. What are some of the advantages of specifying a container for the stl priority_queue?
With the stl priority_queue you can set the underlying container, such as a vector
Share
Setting the underlying container makes it possible to separate out two logically separate concerns:
priority_queueadapter class).As an example, the standard implementation of
vectoris not required to shrink itself down when its capacity is vastly greater than its actual size. This means that if you have a priority queue backed by avector, you might end up wasting memory if you enqueue a lot of elements and then dequeue all of them, since thevectorwill keep its old capacity. If, on the other hand, you implement your ownshrinking_vectorclass that does actually decrease its capacity when needed, you can get all the benefits of thepriority_queueinterface while having the storage be used more efficiently.Another possible example – you might want to change the allocator being used so that the elements of the priority queue are allocated from a special pool of resources. You can do this by just setting the container type of the
priority_queueto be avectorwith a custom allocator.One more thought – suppose that you are storing a
priority_queueof very large objects whose copy time is very great. In that case, the fact that thevectordynamically resizes itself and copies its old elements (or at least, in a C++03 compiler) might be something you’re not willing to pay for. You could thus switch to some other type, perhaps adeque, that makes an effort not to copy elements when resizing and could realize some big performance wins.Hope this helps!