Random access priority queue

后端 未结 2 595
抹茶落季
抹茶落季 2021-01-15 11:29

Continuing List to priority queue

I\'m implementing a improved priority_queue with random access.

template 

        
2条回答
  •  半阙折子戏
    2021-01-15 12:06

    Your pop() method can violate the heap ordering. Use pop_heap() instead of pop_back(). I can't seem to find any other bug right now.

    You can easily test such an implementation by pushing in a random integers and pop() them one by one. You should get them back in sorted order. This is known as heap sort.

    Suggestions:

    • Instead of returning a ref to the container you could implement an const iterator to this class.

    • Note that you should not change the key of the randomly accessed element because it may destroy the heap structure. If you need such functionality you should implement a change_key function which would change the key safely and maintain the heap ordering.

提交回复
热议问题