为什么 std 队列没有定义交换方法专门化
我读到所有 stl 容器都提供了交换算法的专门化,以避免调用默认方法使用的复制构造函数和两个赋值操作。 然而,当我认为在我正在处理的某些代码中使用队列会很好时,我注意到(与向量和双端队列不同)队列不提供此方法? 我刚刚决定使用双端队列而不是队列,但我仍然有兴趣知道这是为什么?
I've read that all stl containers provide a specialisation of the swap algorithm so as to avoid calling the copy constructor and two assignment operations that the default method uses. However, when I thought it would be nice to use a queue in some code I was working on I noticed that (unlike vector and deque) queue doesn't provide this method? I just decided to use a deque instead of a queue, but still I'm interested to know why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++0x 将添加交换到容器适配器,例如 std::queue。 我只能推测为什么当前标准中缺少它。
在此讨论中,有人提出了解决方法:
C++0x will add swap to container adapters like std::queue. I could only speculate why it is missing from the current standard.
In this discussion someone proposes a workaround:
我确信他们被排除在外是因为疏忽。 平心而论,我经常使用 std::queue 和 std::stack 并且从未需要交换两个。 我认为你使用双端队列而不是队列很好。 类似
typedef std::deque的东西 QueueType
应该给出足够的提示如何使用容器。I'm sure that they were left out as an oversight. In all fairness, I use std::queue and std::stack quite a bit and have never had to swap two. I think your use of a deque instead of a queue is fine. Something like
typedef std::deque<MyType> QueueType
should give enough of a hint how the container should be used.