I would like to find a node in my priority queue but I did not find a solution :( If you have a solution, I'm interested.
Thx for help.
I would like to find a node in my priority queue but I did not find a solution :( If you have a solution, I'm interested.
Thx for help.
If you really need to search through a
std::priority_queue
and want to do it efficiently you can derive a new class and add afind
member function. Since you are not adding any additional state you do not have to worry about slicing or other issues sincestd::priority_queue
is not polymorphic.If you do not care about the performance, you can declare an
iterator
to traverse the priority_queue's container. But in C++, the underlying container is been declared asprotected
, and can not be accessed directly.One of my solution to get the iterator of the container is declaring a new class inheriting from
std::priority_queue
.Then you can get the iterator of the container.
In order to be more efficient, for example looking for data by certain keys, you can use
pointers to data
.Suppose the class
Data
holds each item of your data.Data.key
is the key for search andData.value
is the priority inpriority_queue
.Store all your data in a separate collection, for example an array or an link list.
Define a new struct which stores the pointer for certain one
data[i]
Use a
priority_queue
and another data structure support search i.e.binary search tree
,hash
. Here I usemultimap
.Maintain a
priority_queue
ofNode
and amultimap
ofNode
at the same time.Then you can get data's
pointer
by key using multimapd
. The need for priority_queue is also satisfied by using priority_queueq
.All of the above is just using pointers.