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 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:
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.
You can think of s as a restricted deque and q as 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(), and pop_back() for stack and front(), back(), push_back(), and pop_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 with push_front() and pop_front().
See deque for detail.

Queue: you can insert only in one end and remove from the other.
Deque: you can insert and remove from both ends.
So using a Deque, you can model a Queue as well as a Stack.
Hint:
Deque is short for "Double ended queue".

deque is a container template. It satisfies the requirements for a sequence with random-access iterators, much like a vector.
queue is not a container at all, it is an adaptor. It contains a container and provides a different, more specific interface. Use queue when you want to remember (or remind) to avoid operations besides push[_back] and pop[_front], front and back, size and empty. You can't look at elements inside the queue besides the first and last, at all!

In the C++ library, both std::stack and std::queue are implemented as container adapters. That means they provide the interface of a stack or a queue respectively, but neither is really a container in itself. Instead, they use some other container (e.g. std::deque or std::list to actually store the data), and the std::stack class just has a tiny bit of code to translate push and pop to push_back and pop_back (and std::queue does roughly the same, but using push_back and pop_front).

A deque is a double-ended queue, which allows easy insertion/removal from either end. Queues only allow insertion in one end and retrieval from the other.

deque supports insert/pop from back & front
queue only supports insert to the back, and pop from the front. You know, a FIFO (first in first out).

A deque is double-ended. A queue isn't.

Priority queue dequeue happens according to some ordering (priority) comparison not the enqueue order.
For instance you might store timed events in one where you want to pull out the soonest event first and query for its scheduled time so you can sleep until that point in time.
Priority queues are often implemented using heaps.
by Mike Anderson here:
https://www.quora.com/What-is-the-difference-between-a-priority-queue-and-a-queue

In deque(double-ended queue) The element can be inserted from the back and removed from the rear(like in stack), but queue only allows removal from the front.

Related

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

What are the drawbacks of using a std::vector for simulating a queue? I am naively thinking that push_back is used for push and for pop one just stores the position of the first element and increments it. Why does not std::queue allow a std::vector implementation like this in principle (I know the reason is it has no push_front method, but maybe there is something deeper that makes it slow this way)? Thank you for helping.
Why does not std::queue allow a std::vector implementation like this
std::queue is a simple container adapter. It works by delegating pop function to the pop_front function of the underlying container. Vector has no pop front operation, so std::queue cannot adapt it.
but maybe there is something deeper that makes it slow this way
Pushing and popping from the front of the vector is slow because it has to shift all elements which has linear cost. This is why vector doesn't provide pop_front.
stores the position of the first element and increments it.
It's possible to implement a container that does store the position of first element within a buffer, but vector is not an implementation of such container. Storing that position has an overhead that vector doesn't need to pay, and so it doesn't.

Efficieny of stl vector and queue

I want to implement a program using either vector or queue data structure. The function requires frequently pushing and popping some elements (e.g. 8 elements) from the data structure to process these elements, and finally the data structure will be empty. The processing order doesn't matter so both vector and queue are ok. I want to know which one has higher efficiency if I need to frequently push and pop from it. Thank you.
std::queue isn't a container. It is a container adapter. By default, it adapts std::deque.
The function requires frequently pushing and popping some elements ... The processing order doesn't matter
This seems to imply that you can pop from the same end where you push (LIFO). In that case, another container adaptor would be appropriate: std::stack. It also adapts std::deque by default. There is no difference in efficiency of std::vector and the efficiency of std::stack that adapts std::vector. There can be a difference in using std::deque depending on the details of how you use it.

Why is a vector used while defining priority queue of structures?

I know that we can define a priority queue of structures in the following way -
std::priority_queue<somestructure, vector<somestructure>, compare> pq;
Where compare is the structure which contains the compare function. I want to ask why we need to use vector as the second argument in this declaration. How is a vector related to a priority queue while defining the above priority queue?
std::priority_queue is what we call a container adapter. As you know in C++ we got containers like std::vector, std:array or std::deque. All those are there to directly save stuff of type T into them, with different pros and cons.
std::stack for example is a container adapter, which can be used on top of std:deque. The only thing this adapter does is to take away the functionality of std::deque to insert at the end, or take from the end. By this the user is forced to use the std::deque only like a stack.
Something similiar is true for std::priority_queue, it forces you to only insert into the underlying container, a std::vector for example, in an order. By this you get some nice properties of how to find elements in this container. In this special case, by taking more effort into how to insert new elements in the underlying container, and taking away the freedom to randomly insert an element anywhere you like. So you got O(log(n)) complexity for the insert instead of O(1) (instead of inserting at the end for example). But by that you have a complexity of only O(1) to find the largest element, instead of O(n).
There is nothing special about std::vector in this case, you could use any container that satisfy the needs of this container adaptor, you could also use std::deque or your own container or to quote:
The type of the underlying container to use to store the elements.
The container must satisfy the requirements of SequenceContainer, and
its iterators must satisfy the requirements of
LegacyRandomAccessIterator. Additionally, it must provide the
following functions with the usual semantics: front() push_back()
pop_back() The standard containers std::vector and std::deque satisfy
these requirements.

Queue STL in C++ implements the queue using Circular Arrays or Linked Lists

Since the queue can be implemented both ways, I wonder which is being used by the Queue STL.
A queue is a container adaptor; it provides queue-like behavior using a different container for the underlying storage. Either deque or list are suitable for the underlying storage of a queue. See http://eel.is/c++draft/queue for all the gory details.
Neither is a circular array.
Checking some documentation, the complexity of insertion for this type of container is constant, if you use a vector you can't have a constant complexity because you could need resize vector, then it use a linked list.

Why does std::queue not have operator[]?

The std::queue is implemented with a deque by default. std::deque has the subscript operator, operator[], and is probably implemented with arrays. So why doesn't std::queue have operator[]?
I realize you could have a queue with a list as the underlying container. (std::queue<int, std::list<int>>.) But even if that would make the subscript operator slow, is that really a good reason not to include it? That's the only reason I can think of that it is not included.
Because the definition of queue doesn't support such interface. Queue is a FIFO data structure which means First In First Out. Queue supports enqueue and dequeue operations.
Imagine of queue as pipe : you insert data into one end and from the other end you take it out - one by one. Inserting data is called enqueue and taking them out is called dequeue. The C++ standard library has std::queue which defines both these operations: push() is the name of enqueue operation, and dequeue operation has been splitted into two steps namely front() followed by pop(). The rationale why dequeue has been split into two steps is to give strong exception guarantee1.
Wikipedia explains this briefly,
A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.
1. If you want to know how exacly it gives strong exception guarantee, then you can start another topic, because it's very long story, and requires lots of patience to understand it properly. I would suggest you to read Exceptional C++ by Herb Sutter for this.
It's a concept issue. In a queue, you add to the back and take from the front, not from the middle
The reason not to include it because a queue is a data structure with enqueue and dequeue operations, not random access. std::queue exists to adapt an existing container into a queue interface, so it only provides the queue interface.
If you want to use [] then use a deque with push_front and back/pop_back not a queue.
Why a double ended queue has it but not a queue doesn't make sense to me either.