How to iterate over a priority_queue?

priority_queue doesn’t allow iteration through all the members, presumably because it would be too easy in invalidate the priority ordering of the queue (by modifying the elements you traverse) or maybe it’s a “not my job” rationale.

The official work-around is to use a vector instead and manage the priority-ness yourself with make_heap, push_heap and pop_heap. Another work-around, in @Richard’s answer, is to use a class derived from priority_queue and access the underlying storage which has protected visibility.

Leave a Comment