C++ STL Queue Container Adaptor – std::queue

In this tutorial you will learn about STL queue container in C++ or std::queue and all functions which it provides.

std::queue is a container adaptor. Since queue is a container adaptor, this class uses an encapsulated object. It operates in such way that First in First out (FIFO) type of arrangement. Always elements inserted at back side and deleted from the front side of the queue.

C++ STL Queue Container Adaptor – std::queue

To work with queue we need to include queue header file

#include <queue>

The operations that can be applied on queue are:

empty(): This is Boolean function. Returns whether the queue is empty or not.

push(element): This function insert element at back side of the queue.

pop (element):  This function removes the element from queue. Element will be removed from front side.

back(): returns the pointer reference of the last element of the queue.

front(): returns the pointer reference of the first element of the queue.

size(): returns the size of the queue.

Example program to show the above functions:

#include <iostream>
#include <queue>

using namespace std;

void displayQ( queue <int> q1){
	for(int i=0; i<5; i++){
		cout << q1.front() << " ";
		q1.pop();
	}
	cout << endl;
}

int main(){
	queue <int> q1;
	
	for (int i=0; i<5; i++){
		// inserting elements into the queue
		q1.push(i+1);
	}
	
	// calling display function to display elements in the queue
	cout << "the elements in the queue are ";
	displayQ(q1);
	
	cout << "the first element in queue is ";
	cout << q1.front() << endl;
	cout << "the last element in queue is ";
	cout << q1.back() << endl;
	cout << "size of the queue is " ;
	cout << q1.size() << endl;
	
	if (q1.empty()==1){
		cout << "queue is empty" << endl;
	}
	else{
		cout << "queue is not empty " << endl;
	}
	
	return 0;
}

Output

the elements in the queue are 1 2 3 4 5
the first element in queue is 1
the last element in queue is 5
size of the queue is 5
queue is not empty

Swap operation of queues:

This operation swaps the elements of queue1 to queue2 and elements of queue2 to queue1.

Example program to show swap operation:

#include <iostream>
#include <queue>

using namespace std;

void displayQ( queue <int> q1){
	for(int i=0; i<5; i++){
		cout << q1.front() << " ";
		q1.pop();
	}
	cout << endl;
}

int main(){
	queue <int> q1;
	queue <int> q2;
	
	for (int i=0; i<5; i++){
		// inserting elements into the queue 1
		q1.push(i+1);
	}
	
	cout << "elements 1,2,3,4,5 inserted into queue 1" << endl;
	for (int i=0; i<5; i++){
		// inserting elements into the queue 2
		q2.push(i*10);
	}
	
	cout << "elements 0,10,20,30,40 inserted into queue 2" << endl;
	q1.swap(q2);
	cout << "contents after swapping operation" << endl;
	
	cout << "elements of q1 are " << endl;
	displayQ (q1);
	
	cout << "elements of q2 are " << endl;
	displayQ (q2);
	
	return 0;
}

Output

elements 1,2,3,4,5 inserted into queue 1
elements 0,10,20,30,40 inserted into queue 2
contents after swapping operation
elements of q1 are
0 10 20 30 40
elements of q2 are
1 2 3 4 5

Comment below if you have any doubts related to above C++ STL queue container or std::queue.

Leave a Comment

Your email address will not be published. Required fields are marked *