Search code examples
c++stl

priority queue clear method


How do I delete all elements from a priority queue? That means how do I destroy a priority queue? advanced thanks for your answer. Is there any clear- or erase-like method?


Solution

  • The priority_queue interface doesn't have a clear() method (for no good reason I've ever been able to discern). A simple way to clear it is just to assign a new, empty queue:

    priority_queue <int> q;
    // use it
    q = priority_queue <int>(); // reset it