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

后端 未结 5 678
独厮守ぢ
独厮守ぢ 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 19:58

    This is a bit hackish, but nothing illegal about it, and it gets the job done.

    std::make_heap(const_cast(&cities.top()),
                   const_cast(&cities.top()) + cities.size(),
                   Compare());
    

    Update:

    Do not use this hack if:

    • The underlying container is not vector.
    • The Compare functor has behavior that would cause your external copy to order differently than the copy of Compare stored inside the priority_queue.
    • You don't fully understand what these warnings mean.

    You can always write your own container adaptor which wraps the heap algorithms. priority_queue is nothing but a simple wrapper around make/push/pop_heap.

提交回复
热议问题