Can I traverse a standard priority_queue
or standard queue
in c++ with an iterator (like a vector
)? I don't want to use pop because it cause my queue to be dequeued.
Thanks for any help
See Question&Answers more detail:osCan I traverse a standard priority_queue
or standard queue
in c++ with an iterator (like a vector
)? I don't want to use pop because it cause my queue to be dequeued.
Thanks for any help
See Question&Answers more detail:ospriority_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.