我读到所有 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/