Random access priority queue

后端 未结 2 570
抹茶落季
抹茶落季 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

    Doesn't look that great to me:

    • The unary constructor should take argument by const reference.
    • The assignment operator doesn't check for self-assignment.
    • The getContainer() method shows a lack of clarity in the interface - why would you simply expose the implementation detail like that?
    • Most importantly: why do you want a "random access priority queue"?
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题