C++ 优先队列清除方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:16:54  来源:igfitidea点击:

priority queue clear method

c++stl

提问by russell

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?

如何从优先级队列中删除所有元素?这意味着我如何销毁优先队列?高级感谢您的回答。有没有类似清除或擦除的方法?

回答by

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 接口没有 clear() 方法(没有充分的理由我一直能够辨别)。清除它的一种简单方法是分配一个新的空队列:

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

回答by Richard

priority_queuedoesn'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.

priority_queue没有明确的方法。这可能是为了接口简单,或者因为可能存在必须按优先级顺序销毁元素的情况,从而使通用的 clear 函数变得不安全。

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.

无论如何,以下代码块包括两个清除优先级队列的函数。第一种方法是围绕priority_queue 构建一个包装类的临时实例,然后使用它来访问底层存储对象,假设它有一个clear()方法。第二个通过用新队列替换现有的 priority_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;
}

回答by Jonathan Lidbeck

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

这是清除任何priority_queue(和queue,以及大多数其他容器)的干净简单的方法:

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);

回答by Manjunath Bhadrannavar

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

在 C++ 中没有支持 priority_queue 的 clear 方法,但是,下面是清除 priority_queue 的好方法,并且有 O(log(n)) 时间

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

回答by íhor Mé

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 = {};

回答by bhilburn

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/

正如任何 C++ STL 参考将向您展示的那样,STL Priority Queue 类没有像“clear”或“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!

希望这可以帮助!