为什么不能将队列实现为向量?

Why is not possible to have a queue implemented as a vector?

使用 std::vector 模拟队列有什么缺点?我天真地认为 push_back 用于 push 而 pop 只是存储第一个元素的位置并递增它。为什么 std::queue 原则上不允许这样的 std::vector 实现(我知道原因是它没有 push_front 方法,但也许有更深层次的东西使它以这种方式变慢)?谢谢你的帮助。

Why does not std::queue allow a std::vector implementation like this

std::queue 是一个简单的容器适配器。它通过将 pop 函数委托给底层容器的 pop_front 函数来工作。 Vector 没有 pop front 操作,所以 std::queue 无法适配它。

but maybe there is something deeper that makes it slow this way

从向量的前面推入和弹出很慢,因为它必须移动所有具有线性成本的元素。这就是 vector 不提供 pop_front.

的原因

stores the position of the first element and increments it.

可以实现一个将第一个元素的位置存储在缓冲区中的容器,但 vector 不是此类容器的实现。存储该位置有 vector 不需要支付的开销,所以它不需要。