Implementing a priority queue that can be iterated over in C++

后端 未结 4 1495
暗喜
暗喜 2021-02-10 07:23

I need to implement a priority queue for a project, but the STL\'s priority_queue is not indicated since we need to iterate over all elements and remove them random

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 08:21

    You should be able to implement your own priority queue using std::vector, std::make_heap, std::push_heap, and std::pop_heap. Isn't this how std::priority_queue is implemented? You'll just need to call std::make_heap again to fix the data structure when you remove a random element.

    Do you need to iterate over the elements in order? There's a std::sort_heap algorithm to order the underlying std::vector.

提交回复
热议问题