Can I access elements of a Priority Que using an iterator?

为君一笑 提交于 2019-12-05 05:22:36

No, you can't iterate over items in an std::priority_queue. All it supports is inserting items, and removing the highest priority item.

When you want more flexibility, you probably want to use std::make_heap to build the heap structure into your container, std::push_heap to add an item, and std::pop_heap to remove an item.

Since these are algorithms you apply to a container, you can still use the container's iterators as you see fit. Depending on how you modify the data in the heap, you may need to re-build the heap afterwards -- if you modify it in a way that the heap property no longer applies. You can test that with std::is_heap if you have any question.

Aside: many of us find http://www.cppreference.com more useful and accurate than the site you've linked.

Take a look at Boost.Heap. It looks like it addresses at least two of your issues (iteration and mutability).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!