CPP Tutorial

CPP examples programs

CPP Queue

Queue is a data structure in CPP that works on FIFO (First In First Out) technique, which means that the first inserted element will be deleted first in a queue.

Syntax:

template<class T, class Container = deque<T> > class queue;

Member Types of CPP Queue:

MEMBER TYPE USES
container_type To specify underlying container type.
const_reference To specify the reference type of constant container.
size_type To specify the size range of the elements.
value_type To specify the element type.
reference To specify the reference type of container.

 

CPP Queue Functions:

FUNCTION USES
back To access the rear element of the queue.
(constructor) To construct a queue container.
empty To test for the emptiness of a queue and to get a true value if the queue is empty.
emplace To insert a new element in the queue above the current rear end element.
front To access the front element of the queue.
push To insert a new element at the rear end of the queue.
pop To delete the element in the queue from the front end.
swap To interchange the contents of two containers in reference.
size To return the size of the queue container, which is a measure of the number of elements stored in the queue.

 

Example:

#include <iostream.h>  
#include <queue.h>  
using namespace std;  
void display(queue <int> ds)  
{  
queue <int> dis = ds;  
while (!dis.empty())  
{  
	cout << dis.front() << " ";  
	dis.pop();  
}  
}  
 
int main()  
{  
queue <int> getqueue;  
getqueue.push(80);  
getqueue.push(60);  
getqueue.push(50);  
getqueue.push(30);  
 
cout << "The Queue element from first to last are  : ";  
display(getqueue);  
 
cout << "\nThe value of getqueue.size() : " << getqueue.size();  
cout << "\nThe value of getqueue.front() : " << getqueue.front();  
cout << "\nThe value of getqueue.back() : " << getqueue.back();  
 
cout << "\nThe value of getqueue.pop() : ";  
getqueue.pop();  
display(getqueue);  
 
return 0;  
}

Output

The Queue element from first to last are : 80 60 50 30                                          
The value of getqueue.size() : 4                      
The value of getqueue.front() : 80                    
The value of getqueue.back() : 30   
The value of getqueue.pop() : 60 50 30