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:


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:


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 *