为什么 std 队列没有定义交换方法专门化

发布于 2024-07-23 02:10:06 字数 147 浏览 6 评论 0原文

我读到所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-07-30 02:10:06

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:

There is a solution since the standard makes the needed parts protected,
called inheritance. [just don't destruct via the std adaptors]
create a templated struct inheriting the desired adaptor, provide just
the constructors and forward the args to the adaptor class, writing a
swap member is a snap since the required items are protected members of
the standard adaptors.

苏佲洛 2024-07-30 02:10:06

我确信他们被排除在外是因为疏忽。 平心而论,我经常使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文