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 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.
For example:
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.
You can think of
sas a restricted deque andqas a restricted list.All that is necessary is that the lower level container implements the methods needed by the higher level container. These are
back(),push_back(), andpop_back()for stack andfront(),back(),push_back(), andpop_front()for queue.See stack and queue for more detail.
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 withpush_front()andpop_front().See deque for detail.