visual-studio 为什么 std::queue 不支持 clear() 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3874624/
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-10-22 12:33:07  来源:igfitidea点击:

Why doesn't std::queue support a clear() function?

c++visual-studiovisual-c++stl

提问by bjskishore123

I have requirement: for a function, I get the input as a stream of numbers. I mean, the function keeps on getting called with single number in each call. I am using std::queuefor storing the stream of numbers. I need to process a collected set of numbers only when some condition is satisfied. If the condition is not satisfied I need to put all the elements into the queue and then start storing new numbers in there. For emptying the queue, I couldn't find a clear()method. So I am looping like this:

我有要求:对于一个函数,我将输入作为数字流。我的意思是,该函数在每次调用中都会继续使用单个号码进行调用。我std::queue用于存储数字流。只有在满足某些条件时,我才需要处理一组收集的数字。如果条件不满足,我需要将所有元素放入队列,然后开始在其中存储新数字。为了清空队列,我找不到clear()方法。所以我是这样循环的:

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

I got an efficient algorithm for clearing a std::queueat

我得到了一个有效的算法来清除std::queueat

How do I clear the std::queue efficiently?

如何有效地清除 std::queue?

My question is: Why doesn't std::queuesupport a clear()function?

我的问题是:为什么不std::queue支持clear()功能?

Since std::dequeand std::vectorboth support a clear()method, what is the technical difficulty in supporting it for std::queue?

既然std::dequestd::vector都支持一个clear()方法,那么支持它的技术难点是什么std::queue

Or is my above use case very rare and hence not supported?

或者我的上述用例非常罕见,因此不受支持?

采纳答案by SirDarius

According to http://www.cplusplus.com/reference/stl/queue/,

根据http://www.cplusplus.com/reference/stl/queue/

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access it elements.

队列被实现为容器适配器,这些类使用特定容器类的封装对象作为其底层容器,提供一组特定的成员函数来访问它的元素。

which means that the queue uses an already existing container, and is just really is an interface to this container as a FIFO queue.

这意味着队列使用一个已经存在的容器,并且实际上是作为 FIFO 队列的这个容器的接口。

This means queues are not meant to be cleared. If you need to clear a queue, this means you actually need to use an object that is not a queue, and therefore you should instead use the actual underlying container type, being a deque by default.

这意味着队列不应该被清除。如果你需要清除一个队列,这意味着你实际上需要使用一个不是队列的对象,因此你应该使用实际的底层容器类型,默认情况下是一个双端队列。

回答by sellibitze

Apart from what has been said already, you canclear a queue very easily:

除了已经说过的内容之外,您还可以非常轻松地清除队列:

queue<int> q;
...
q = queue<int>(); // Assign an empty queue

or in C++11

或在 C++11 中

q = {};

回答by Steve Townsend

queueis just an adapter for some underlying container, by default a deque, with restricted function (as you noted here). If you want the full blown function use the underlying dequeinstead of queue.

queue只是一些底层容器的适配器,默认情况下 a deque,具有受限功能(如您在此处所述)。如果您想要完整的功能,请使用底层deque而不是queue.

回答by Andy Krouwel

Added this to my growing list of 'make STL readable' functions:

将此添加到我不断增长的“使 STL 可读”功能列表中:

template <typename T> 
void Clear(std::queue<T>& Queue) 
{
    Queue = std::queue<T>(); // Assign to empty queue
}

It's just a wrapper around sellibitze's excellent answer, but means I don't have to also add a comment every time I use the technique.

这只是对 sellibitze 出色答案的一个包装,但这意味着我不必在每次使用该技术时都添加评论。