queue<T, Sequence>

Categories: containers, adaptors

Component type: type

Description

A queue is an adaptor that provides a restricted subset of
Container functionality
A queue is a "first in first out" (FIFO) data structure. [1] That is,
elements are added to the back of the queue and may be removed
from the front;
Q.front() is the element that was added to the queue least recently.
Queue does not allow iteration through its elements. [2]

Queue is a container adaptor, meaning that it is implemented on
top of some underlying container type. By default that underlying
type is deque, but a different type may be selected explicitly.

[2]
This restriction is the only reason for queue to exist at all.
Any container that is both a front insertion sequence and a
back insertion sequence can be used as a queue; deque, for
example, has member functions front, back, push_front,
push_back, pop_front, and pop_back The only reason to use the
container adaptor queue instead of the container deque is to
make it clear that you are performing only queue operations, and no
other operations.

[3]
One might wonder why pop() returns void, instead of
value_type. That is, why must one use front() and pop() to
examine and remove the element at the front of the queue, instead of
combining the two in a single member function? In fact, there is a
good reason for this design. If pop() returned the front element, it
would have to return by value rather than by reference: return by
reference would create a dangling pointer. Return by value, however,
is inefficient: it involves at least one redundant copy constructor
call. Since it is impossible for pop() to return a value in such a
way as to be both efficient and correct, it is more sensible for it to
return no value at all and to require clients to use front() to
inspect the value at the front of the queue.