开发者

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 an开发者_JS百科swer. Is there any clear- or erase-like method?


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


priority_queue doesn't have a clear method. It may be that this is for interface simplicity, or because it there may be situations in which elements must be destroyed in priority-order, making a generic clear function unsafe.

Regardless, the following code block includes two functions to clear priority queues. The first works by building a temporary instance of a wrapper class around the priority_queue and then using this to access the underlying storage object, which is assumed to have a clear() method. The second works by replacing the existing priority_queue with a new queue.

I use templating so that the functions can be recycled time and again.

#include <queue>
#include <iostream>
using namespace std;

template <class T, class S, class C>
void clearpq(priority_queue<T, S, C>& q) {
    struct HackedQueue : private priority_queue<T, S, C> {
        static S& Container(priority_queue<T, S, C>& q) {
            return q.*&HackedQueue::c;
        }
    };
    HackedQueue::Container(q).clear();
}

template <class T, class S, class C>
void clearpq2(priority_queue<T, S, C>& q){
    q=priority_queue<T, S, C>();
}

int main(){
    priority_queue<int> testq, testq2;

    //Load priority queue
    for(int i=0;i<10;++i)
        testq.push(i);

    testq2=testq;

    //Establish it is working
    cout<<testq.top()<<endl;
    testq.pop();
    cout<<testq.top()<<endl;
    testq.pop();

    //Clear it and prove that it worked
    clearpq(testq);
    cout<<testq.size()<<endl;

    //Use the second clearing function
    cout<<testq2.size()<<endl;
    clearpq2(testq2);
    cout<<testq2.size()<<endl;
}


Here's a clean and simple method to clear any priority_queue (and queue, and most other containers as well):

template <class Q>
void clearQueue(Q & q) {
    q = Q();
}

Since it's a template, you don't have to remember all the template parameters.

Example:

std::priority_queue<MyType> simpleQueue;
std::priority_queue<MyType, std::deque<MyType>, MyHashFunction> customQueue;

// ... later ...

clearQueue(customQueue);
clearQueue(simpleQueue);


As any C++ STL reference will show you, the STL Priority Queue class does not have a function like 'clear' or 'erase'. http://www.cplusplus.com/reference/stl/priority_queue/

It is a container class, and as such, a very simple destructor is generated by the compiler (in most cases). If your priority queue uses only locally-allocated information in its nodes, then this should work fine for clearing out the memory.

However, if you have dynamically allocated memory for the information in your priority queue, you will need to manually create a 'clear'-like function.

Hope this helps!


priority_queue<int> a;
a.push(10);
a.push(9);
a.push(8);
a   =   {};
a.push(1);
a.push(4);
a.push(6);

while(!a.empty())
    {
    std::cout<< a.top();
    a.pop();
    }

results in

641

So you can simply

a = {};


there is no clear method supported for priority_queue in c++ but, this below is a good method to clear the priority_queue and has O(log(n)) time

while (!pq.empty())
      pq.pop();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜