为什么标准C ++容器适配器不提供清晰的功能? [英] Why don't the standard C++ container adaptors provide a clear function?

查看:120
本文介绍了为什么标准C ++容器适配器不提供清晰的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么std :: queue,std :: stack和std :: priority_queue不提供 clear()成员函数?我必须伪造一个这样的:

Does anyone know why std::queue, std::stack, and std::priority_queue don't provide a clear() member function? I have to fake one like this:

std::queue<int> q;
// time passes...
q = std::queue<int>();  // equivalent to clear()

IIRC, clear c $ c>由可以充当底层容器的所有内容提供。有没有很好的理由没有容器适配器提供它?

IIRC, clear() is provided by everything that could serve as the underlying container. Is there a good reason to not have the container adaptors provide it?

推荐答案

嗯,我想这是因为 clear 不被认为是队列,priority_queue或堆栈上的有效操作(顺便说一句,deque不是,而是适配器,而是容器)。

Well, I think this is because clear was not considered a valid operation on a queue, a priority_queue or a stack (by the way, deque is not and adaptor but a container).


使用容器
适配器队列而不是容器
deque的唯一原因是,你是
只执行队列操作,
没有其他操作。 (来自队列上的sgi页面)

所以当使用队列时,你可以做的是push / pop元素;清除队列可以被视为违反FIFO概念。因此,如果你需要清除你的队列,也许它不是一个队列,你应该更好地使用deque。

So when using a queue, all you can do is push/pop elements; clearing the queue can be seen as a violation of the FIFO concept. Consequently, if you need to clear your queue, maybe it's not really a queue and you should better use a deque.

然而,这个概念有点狭窄,我认为你应该清除队列是公平的。

However, this conception of things is a little narrow-minded, and I think clearing the queue as you do is fair enough.

这篇关于为什么标准C ++容器适配器不提供清晰的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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