C ++队列与队列 [英] c++ deque vs queue vs stack
问题描述
队列和堆栈是被广泛提及的结构。但是,在C ++中,对于队列,您可以通过两种方式进行:
Queue and Stack are a structures widely mentioned. However, in C++, for queue you can do it in two ways:
#include <queue>
#include <deque>
但是对于堆栈,您只能这样做
but for stack you can only do it like this
#include <stack>
我的问题是,队列和双端队列有什么区别,为什么要提出两个结构?对于堆栈,可以包含任何其他结构吗?
My question is, what's the difference between queue and deque, why two structures proposed? For stack, any other structure could be included?
推荐答案
Moron / Aryabhatta是正确的,但是稍微详细一点可能会有所帮助。
Moron/Aryabhatta is correct, but a little more detail may be helpful.
队列和堆栈是比双端队列,向量或列表更高级别的容器。通过这个,我的意思是您可以在较低级别的容器中构建队列或堆栈。
Queue and stack are higher level containers than deque, vector, or list. By this, I mean that you can build a queue or stack out of the lower level containers.
例如:
std::stack<int, std::deque<int> > s;
std::queue<double, std::list<double> > q;
将使用双端队列作为基础容器构建一堆整数,并使用列表构建双打队列
Will build a stack of ints using a deque as the underlying container and a queue of doubles using a list as the underlying container.
您可以将 s
视为受限双端队列和 q
作为受限列表。
You can think of s
as a restricted deque and q
as a restricted list.
所有必要的操作是较低级别的容器实现较高级别的容器所需的方法。它们是 back()
, push_back()
和 pop_back()
用于堆栈和 front()
, back()
, push_back()
和 pop_front()
作为队列。
All that is necessary is that the lower level container implements the methods needed by the higher level container. These are back()
, push_back()
, and pop_back()
for stack and front()
, back()
, push_back()
, and pop_front()
for queue.
See stack and queue for more detail.
关于双端队列,您不仅可以在两个队列中插入队列结束。特别是,它具有随机访问 operator []
。这使其更像一个向量,但它是一个向量,您可以使用 push_front()
和 pop_front()$开始插入和删除c $ c>。
With respect to the deque, it is much more than a queue where you can insert at both ends. In particular, it has the random access operator[]
. This makes it more like a vector, but a vector where you can insert and delete at the beginning with push_front()
and pop_front()
.
请参见 deque 了解详情。
这篇关于C ++队列与队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!