queue<T>

The STL queue is a container adapter that represents a first-in, first-out (FIFO) queue. Only the oldest inserted element in the queue can be accessed.

queue is a container adapter, not a true STL container, and is implemented in terms of another linear container class like list or deque. To explicitly specify which container to use in the queue, parametrize the queue as queue<T, ContainerType<T> >. Note that you cannot build a queue around a vector, since vector does not have a pop_front member function.

Common queue<T> Member Functions
void push(const T& elem)
Insert element
myQueue.push(137); // Push 137 onto the queue

Enqueues a new element into the queue.

void pop();
Remove first element
myQueue.pop(); // Note: no return value

Dequeues the first element of the queue. The behavior is undefined if the queue has no elements, so be sure not to pop off of an empty queue. Note that pop does not return a value; use front to get the value before popping.

T& front() const
const T& front() const
Return first element
int firstElem = myQueue.front();

Returns the first element of the queue (the one that will be removed by pop). The behavior is undefined if the queue has no elements, so be sure not to access the front element of an empty queue. The front function returns a reference to the front element of the queue, and you can modify the element if you so choose.

size_type size() const
Get container size
int numElems = myQueue.size();

Returns the number of elements stored in the queue.

bool empty() const
Is queue empty?
if(myQueue.empty()) { ... }

Returns whether the queue is empty. It's considered good practice to call empty to determine whether the queue is empty rather than checking whether size() returns zero.

Other queue<T> Member Functions
Constructor: queue<T, Container<T> > (const Container<T> &initial = Container<T>())
Constructs a new queue around an existing container.
queue<int, list<int> > myQueue(myList); // Queue contents initialized to those of myList

Constructs a new queue whose contents are equal to those of the specified container. This constructor is rarely used, but is useful if you already have the data you want to use in the queue.

T& back() const
const T& back() const
Return last element
int lastElem = myQueue.back();

Returns the last element of the queue (the one most recently added). The behavior is undefined if the queue has no elements, so be sure not to access the back element of an empty queue. The back function returns a reference to the back element of the queue, and you can modify the element if you so choose.