How to tell a std::priority_queue to refresh its ordering?

后端 未结 5 674
独厮守ぢ
独厮守ぢ 2020-12-14 19:35

I have a priority queue of pointers to a struct city. I modify the objects pointed by these pointers outside the priority queue, and want to tell the priority q

5条回答
  •  一生所求
    2020-12-14 20:07

    If you need to keep an ordered collection you may consider the following solution: Use std::set and to update values remove the item, update its value and place it back into the set. This will give you O(log n) complexity for updating one item, which is the best you can in a usual heap structure (Assuming you had access to its internals to mass with the sift-up sift-down procedures). The only downside to std::set will be the time for initializing a set with n items. (O(n log n) instead of O(n)).

提交回复
热议问题