[英]Why doesn't std::queue support a clear() function?
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::queue
for storing the stream of numbers.我正在使用std::queue
来存储数字流。 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.为了清空队列,我找不到clear()
方法。 So I am looping like this:所以我是这样循环的:
while(!q.empty())
q.pop();
I got an efficient algorithm for clearing a std::queue
at我得到了一个有效的算法来清除std::queue
在
How do I clear the std::queue efficiently? 如何有效地清除 std::queue?
My question is: Why doesn't std::queue
support a clear()
function?我的问题是:为什么std::queue
支持clear()
函数?
Since std::deque
and std::vector
both support a clear()
method, what is the technical difficulty in supporting it for std::queue
?由于std::deque
和std::vector
都支持clear()
方法,那么为std::queue
支持它的技术难点是什么?
Or is my above use case very rare and hence not supported?或者我的上述用例非常罕见,因此不受支持?
Apart from what has been said already, you can clear a queue very easily:除了已经说过的内容之外,您还可以非常轻松地清除队列:
queue<int> q;
...
q = queue<int>(); // Assign an empty queue
or in C++11或在 C++11 中
q = {};
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.如果你需要清除一个队列,这意味着你实际上需要使用一个不是队列的对象,因此你应该使用实际的底层容器类型,默认情况下是一个双端队列。
queue
is just an adapter for some underlying container, by default a deque
, with restricted function (as you noted here). queue
只是一些底层容器的适配器,默认情况下是deque
,具有受限功能(如您在此处所述)。 If you want the full blown function use the underlying deque
instead of queue
.如果您想要完整的功能,请使用基础deque
而不是queue
。
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 出色答案的一个包装,但这意味着我不必在每次使用该技术时都添加评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.