cppreference.com > C++ Queues
C++ Queues

Queue constructor
Syntax:
  #include <queue>
  queue();
  queue( const Container& con );

Queues have an empty constructor and a constructor that can be used to specify a container type.


back
Syntax:
  #include <queue>
  TYPE& back();
  const TYPE& back() const;

The back() function returns a reference to the last element in the queue.

For example:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 cout << "The first element is " << v.front()
      << " and the last element is " << v.back() << endl;		
		

This code produces the following output:

 The first element is 0 and the last element is 4		
		
		
		

The back() function runs in constant time.


empty
Syntax:
  #include <queue>
  bool empty() const;

The empty() function returns true if the queue has no elements, false otherwise.

For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a queue and display its contents in reverse order:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout << v.back() << endl;
   v.pop_back();
 }		
		
		


front
Syntax:
  #include <queue>
  TYPE& front();
  const TYPE& front() const;

The front() function returns a reference to the first element of the queue, and runs in constant time.


pop
Syntax:
  #include <queue>
  void pop();

The function pop() removes the top element of the queue and discards it.


push
Syntax:
  #include <queue>
  void push( const TYPE& val );

The function push() adds val to the end of the current queue.

For example, the following code uses the push() function to add ten integers to the end of a queue:

   queue<int> q;
   for( int i=0; i < 10; i++ )
     q.push(i);		


size
Syntax:
  #include <queue>
  size_type size() const;

The size() function returns the number of elements in the current queue.