std::priority_queue<std::vector> how to reserve space in the internal vector container

I am trying to performance optimize some code and I want to observe the effect of reserving space on queue

I have a priority queue (https://en.cppreference.com/w/cpp/container/priority_queue) :
 
std::priority_queue<Node *, std::vector<Node *>, CompareNode> QUEUE;


which as you see it made out of a vector I want to reserve some space on the underling vector container

The que's inner container is protected therefore I can not access it.
 
itsPQ.c.reserve(10); //std::priority_queue.c is protected 


is there a different way (that works) to reserve space for the vector

Note: the QUEUE is part of a shallow graph's A* search

Thanks in advance
Last edited on
there are several techniques here. I have not experimented with any of them (I admit to not caring for some of the STL containers, like this one, which bring nothing but limitations with them. you can do a PQ in a vector or list without the limitations).

https://stackoverflow.com/questions/29235978/how-to-preallocatereserve-a-priority-queuevector
Last edited on
Is there a different way to reserve space for the vector

From Jonnin's link, I guess there is. TIL and thanks.

Honestly I suggest that you get rid of priority_queue, it's inadequate. Instead, call std::push/pop/make/sort_heap yourself. I quit using std::priority_queue several years ago, because every time I tried to use it, I had to rip it out later.
Last edited on
if you want performance, a 2-d thing of [priority class][normal queues or list/vector/other as-a-q] should be easy to cook up without having to do much more than wrap it (if you even want to stuff it into an object, not necessary to do that either). Cuts out the sorting via the bucket idea. It may not be memory efficient unless you really dig into rolling something out.
Last edited on
I ended up going with boost::heap::priority_queue which (at least for my purpose) is a drop-in replacement for std::priority_queue()
https://www.boost.org/doc/libs/1_74_0/doc/html/boost/heap/priority_queue.html

It contains reserve() and clear()

from what I can tell it lacks the ability to use a custom container though (it uses std::vector)
Registered users can post here. Sign in or register to post.