为什么std :: queue不支持clear()函数? [英] Why doesn't std::queue support a clear() function?

查看:169
本文介绍了为什么std :: queue不支持clear()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 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:

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

我在

如何有效清除std :: queue?

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

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

由于std::dequestd::vector都支持clear()方法,因此std::queue支持该技术的技术难度是什么?

Since std::deque and std::vector both support a clear() method, what is the technical difficulty in supporting it for std::queue?

还是我的上述用例非常少见,因此不被支持?

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

推荐答案

根据 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.

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

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

这意味着不清除队列.如果需要清除队列,则意味着您实际上需要使用不是队列的对象,因此应改为使用实际的基础容器类型,默认情况下为双端队列.

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.

这篇关于为什么std :: queue不支持clear()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆