Forgotten Password?

Or login with:

  • Facebook
  • Google
  • Yahoo


A queue is an Adaptor that provides a restricted subset of Container functionality.


The queue is defined in the standard header <queue> and in the nonstandard backward-compatibility header <queue.h>.

namespace std{
   template < class T, class Container = deque<T> > 
   class queue;


A queue is a Container Adaptor that allows data to be added at one end and taken out of the other end. The deque interface is restricted (i.e., much of it is hidden) so that the required First In First Out (FIFO) queue-like behavior is provided.

Queue overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove an element from an empty queue.

A bounded queue is a queue limited to a fixed number of items.

A simple example of instantiating a queue is:

queue <int> qI; // a queue of integers
queue <double> qD; // a queue of doubles
// a queue of doubles stored internally in a list
queue <double, list <double> > qDoublesInList;


Queue is a container adaptor, meaning that it is implemented on top of some underlying container type like deque and list. By default, that underlying type is deque, but a different type may be selected. (see Example 2)

Queue Operations

Create, Copy and Destroy Operations

Operation Effect
queue< El > c Creates an empty queue c which can hold values of type El
queue< El> c1(c2) Creates c1 as a copy of c2, whose component type must be El
queue< El > c1 = c2 Copy constructor (alternate usage syntax)

Note: 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 involved when the queue goes out of scope.

Nonmodifying Operations of Queues
Operation Effect
c1==c2 Returns if c1 is equal to c2
c1!=c2 Returns if c1 is not equal to c2
c1<c2 Returns if c1 is less than c2
c1<=c2 Returns if c1 is less than or equal to c2
c1>c2 Returns if c1 is greater than c2
c1>=c2 Returns if c1 is greater than or equal to c2
c.front() Returns a reference to the front end component of c
c.back() Returns a reference to the back end component of c
c.size() Returns a value of type size_type giving the number of values currently in c
c.empty() Returns true if c is empty (contains zero values), otherwise return false

Modifying Operations of Stacks
Operation Effect
c1=c2 Assigna c2 to c1, and returns the common value. The queue on the left of an assignment receives the values and size of the one on the right
c.push(val) Adds val to the back end of c, increasing the size of c by one
c.pop() Removes the front end value of c, decreasing size of c by one


Example - simple queue of characters
This program illustrates the FIFO behavior of a simple queue of characters, as well as its default constructor, its copy constructor, and the queue push(), pop(), front(), back(),empty(),size() member functions.
using namespace std;
int main
  queue<char> queue1;
  cout <<"\nThe queue1 contains "<<queue1.size()<<" values.";
  cout <<"\nThe front value is "<<queue1.front()<<" and the back  value is "<<queue1.back()<<".";
  cin.ignore(80, '\n');
  queue<char> queue2(queue1);
  cout <<"The queue2 is created as a copy of queue1, after which another value is added,so its size
is "<<queue2.size()<<".";
  cout <<"\nThe front value is "<<queue2.front()<<"and the back value is "<<queue2.back()<<".";
  cin.ignore(80, '\n');
  cout <<"\nValues of queue1, in FIFO order:\n";
   cout <<queue1.front()<<"\n";
  cin.ignore(80, '\n');
  cout <<"\nValues of queue2, in FIFO order:\n";
   cout <<queue2.front()<<"\n";
  cin.ignore(80, '\n');
  return 0;

The queue1 contains 3 values.
The front value is a and the back value is c.

The queue2 is created as a copy of queue1, after which another value is added, so its size is 4. The front value is a and the back value is d.

Values of queue1, in FIFO order:

Values of queue2, in FIFO order:

Last Modified: 11 Dec 11 @ 11:08     Page Rendered: 2022-03-14 16:02:22