Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL Priority Queue - deleting an item

I want to implement a timer queuing system using the C++ STL priority_queue container adapter.

My problem is that I want to occasionally cancel a timer, however there are no interfaces that enable me to easily delete an item in the priority_queue that is not the top item.

Any suggestions?.

Thank you for your help.

like image 837
Umbungu Avatar asked Jun 19 '10 16:06

Umbungu


People also ask

How do I remove a specific item from a priority queue?

The remove() method of PriorityQueue class of java. util package is used to remove a particular element from a PriorityQueue.

How do I remove an item from the queue?

To remove an element from a queue, bring that element to the front of the Queue and pop it out using the pop() function.

Can we delete a particular element from priority queue in C++?

CPP. pop() function is used to remove the top element of the priority queue.


1 Answers

I'm afraid STL priority_queue doesn't offer such functionality. You can write your own heap class (which is not that hard). You could even use the std::xxx_heap functions by dirty tricks like this:

delete_heap(iterator to_delete, iterator heap_begin, iterator heap_end)
{
  to_delete->key = something that would compare less to everything; // make sure it gets to the top in the next step
  std::push_heap(heap_begin, to_delete+1);
  std::pop_heap(heap_begin, heap_end);
}

which will get you O(log n) delete.

like image 58
jpalecek Avatar answered Oct 04 '22 22:10

jpalecek