Setting up the base container allows you to identify two logically separate problems:
- How do you store the actual items that make up the priority queue (container), and
- How to organize these elements to efficiently execute the priority queue (adapter class
priority_queue ).
As an example, the standard implementation of vector does not need to shrink when its capacity is significantly larger than its actual size. This means that if you have a priority queue supported by vector , you can end up losing memory if you insert a lot of elements and then delete them all, since vector will keep its former capacity. If, on the other hand, you implement your own shrinking_vector class, which really reduces its capacity when necessary, you can get all the advantages of the priority_queue interface when using the storage more efficiently.
Another possible example is that you can change the allocator used so that the priority queue elements are allocated from a special resource pool. You can do this by simply setting the container type priority_queue as vector using a special allocator.
Another thought - suppose you store priority_queue very large objects, the copy time of which is very long. In this case, the fact that vector dynamically resizes and copies its old elements (or at least in the C ++ 03 compiler) may be what you are not willing to pay for. Thus, you can switch to some other type, perhaps deque , which makes efforts not to copy elements when resizing and could not achieve big performance gains.
Hope this helps!
templatetypedef
source share