c++ - 为什么标准队列没有定义交换方法特化

标签 c++ stl queue swap stl-algorithm

我读到所有 STL 容器都提供交换算法的专门化,以避免调用默认方法使用的复制构造函数和两个赋值操作。但是,当我认为在我正在处理的某些代码中使用队列会很好时,我注意到(与 vector 和 deque 不同)队列不提供这种方法?我只是决定使用双端队列而不是队列,但我仍然很想知道这是为什么?

最佳答案

C++0x将交换添加到容器适配器,如 std::queue。我只能推测为什么当前标准中缺少它。 在 this discussion有人提出了解决方法:

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.

关于c++ - 为什么标准队列没有定义交换方法特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/937621/

相关文章:

python - Pybind11 类型错误

c++ - std::list 与 std::vector 迭代

c++ - 通过引用返回 STL 映射的正确方法/此行为是否定义明确?

javascript - Kue:队列为空时通知的事件

python - 在 Heroku 上的 RQ 中处理异常处理和重新排队

c++ - 关于删除指针的 Clang 警告

c++ - is_integral vs is_integer : is one of them redundant?

python - 为什么 python 队列没有 false 行为

c++ - 将大型 C++ 程序拆分为多个组件

c++ - 如何遍历基类对象的 vector ?