The STL queue is a container adaptor. That is, it is not a "first-class" container, but instead simply "adapts" one of the sequential first-class containers (by default, the deque) for its own purposes. So, the deque interface is restricted (i.e., much of it is hidden) so that the required FIFO (First In, First Out) queue-like behavior is provided.

Constructors and destructor

Default Constructor

queue<T> q;
Construct an empty queue q which can hold values of type T.

Copy Constructor

There is, of course, only one copy constructor, but there are two syntactic forms that invoke it, and both are shown.

queue<T> q(otherQueue);
Construct q as a copy of otherQueue, whose component type must be T.
queue<T> q = otherQueue;
Copy constructor (alternate usage syntax).

Destructor

Any queue will have a container data member (by default, a deque) which will hold its elements. That data member will have its own destructor which will automatically be invoked when the queue goes out of scope.

Overloaded operators

Assignment operator

q1 = q2
Assign q2 to q1, and return the common value. The queue on the left of an assignment receives the values and size of the one on the right.

Relational operators

Queues are compared in the "lexicographic ordering" sense. This essentially means that the two queues are compared by comparing their values pairwise, starting at the beginning, with each comparison looking at two values in corresponding positions, until a determination of the relationship between the two queues can be made. Only queues of the same component type can be compared of course, and the == and < operators must be defined for the component type.

q1 == q2
Return true if q1 and q2 have the same component type and the same size, and the components in each pair of corresponding locations have the same value; otherwise return false.
q1 != q2
Return true if q1==q2 returns false; otherwise return false.
q1 < q2
Return true if, in the pairwise comparison of values from q1 and q2, in the first pair in which the two values differ, the one from q1 is less than the one from q2; otherwise return false.
q1 <= q2
Return true if either q1<q2 or q1==q2 is true; otherwise return false.
q1 > q2
Return true if q2<q1 is true; otherwise return false.
q1 >= q2
Return true if either q1>q2 or q1==q2 is true, otherwise return false.

Member functions for accessing a value

q.front()
Return a reference (or const_reference) to the front end component of q.
q.back()
Return a reference (or const_reference) to the back end component of q.

Member functions for reporting status

q.size()
Return a value of type size_type giving the number of values currently in q.
q.empty()
Return true if q is empty (contains zero values); otherwise return false.

Member function for inserting a value

q.push(val)
Add val to the back end of q, increasing the size of q by one.

Member function for deleting a value

q.pop()
Delete the front end value of q, decreasing size of q by one.

Miscellaneous notes

Implementation
Standard C++ does not say how the STL containers and algorithms must be implemented. It does, however, state certain constraints, such as complexity constraints, to which each implementation must adhere. Thus it is much better to base your programs on the STL's performance guarantees, rather than upon any assumption about how a particular feature, like the queue class, may be implemented. The queue container adaptor is based by default on the deque. A list could also be used, since both provide the push_back(), pop_front(), front() and back() operations that are necessary to support the queue interface.

Sample programs

All programs have been compiled and run successfully under Microsoft Visual Studio .NET 2005, unless otherwise noted.

queue01.cpp | Windows_executable | program_output (text)
Illustrates the "FIFO" (First In, First Out) behavior of a simple queue of characters, as well as its default constructor, its copy constructor, and the queue push(), pop(), front(), back(), empty(), and size() member functions.
queue02.cpp | Windows_executable | program_output (text)
Illustrates the creation of a queue using values from a deque (when the underlying container is the default one, namely a deque), and from a list, when the underlying container is a list.
queue03.cpp | Windows_executable | program_output (text)
Illustrates the assignment of one queue to another, and the comparison of queues.
queue04.cpp | Windows_executable | program_output (text)
Illustrates how *not* to access the components of a queue.

Member function prototypes

Template specification for the queue class

template<class T,
         class Allocator = allocator<T> >
class queue { ... }

Default Constructor

explicit queue(const Container& c = Container());

Copy Constructor

queue(const Container& otherContainer);

Destructor

~queue();

back

      T& back() const;
const T& back() const;

empty

bool empty() const;

front

      T& front() const;
const T& front() const;

pop

void pop();

push

void push(const T& val);

size

size_type size() const;