Tyson Tyson - 3 years ago 58
C++ Question

How to maintain a reference to a std::priority_queue's container?

I'm creating a

std::priority_queue
using
std::vector
as container.

It seems like the priority queue creates a copy of the container passed to it in its constructor, since changes made to the container after constructing the queue aren't reflected in the queue's container.

For example, if I call
clear()
on my container, the priority queue remains full.

Is there a way to maintain a reference to the priority queue's internal container after construction?

Answer Source

No, there is no way of doing that. The reason being is that if you had access to the underlying container, you could access it and modify it like it wasn't a priority queue, which would be very confusing and against the wanted use case of std::priority_queue, which is to provide a queue.

This is from the Standards point of view, maybe your implementation has an extension that allows that access, but that would be very surprising, and I know no implementation that does this. You should consult the appropriate documentation to be sure.

Having said that, you can always provide a wrapper around std::priority_queue, which stores the container. But I would not recommend it, (as said above) you would be able to change it without the queue semantics.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download