Does changing a priority queue element result in resorting the queue?

前端 未结 4 1647
抹茶落季
抹茶落季 2021-01-02 16:58

I have a priority_queue, and I want to modify some of it\'s contents (the priority value), will the queue be resorted then?

It depends if it resorts on push/pop (mor

4条回答
  •  庸人自扰
    2021-01-02 17:29

    I stumbled on this issue while considering the use of priority queues for an A* algorithm.

    Basically, C++ priority queues are a very limited toy.

    Dynamically changing the priority of a queued element requires to perform a complete reconstruction of the underlying heap manually, which is not even guaranteed to work on a given STL implementation and is grossly inefficient.
    Besides, reconstructing the heap requires butt-ugly code, which would have to be hidden in yet another obfuscated class/template.

    As for so many other things in C++, you'll have to reinvent the wheel, or find whatever fashionable library that reinvented it for you.

提交回复
热议问题