Skip to content
Related Articles

Related Articles

Queue in C++ Standard Template Library (STL)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 18 May, 2022

Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. Queues use an encapsulated object of deque or list (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.


Following is an example to demonstrate the queue and its various methods.


// CPP code to illustrate Queue in 
// Standard Template Library (STL)
#include <iostream>
#include <queue>
using namespace std;
// Print the queue
void showq(queue<int> gq)
    queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.front();
    cout << '\n';
// Driver Code
int main()
    queue<int> gquiz;
    cout << "The queue gquiz is : ";
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.front() : " << gquiz.front();
    cout << "\ngquiz.back() : " << gquiz.back();
    cout << "\ngquiz.pop() : ";
    return 0;


The queue gquiz is :     10    20    30

gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() :     20    30

Methods of Queue are: 

Method Definition
queue::empty() Returns whether the queue is empty.
queue::size() Returns the size of the queue.
queue::swap() Exchange the contents of two queues but the queues must be of the same type, although sizes may differ.
queue::emplace() Insert a new element into the queue container, the new element is added to the end of the queue.
queue::front() Returns a reference to the first element of the queue.
queue::back() Returns a reference to the last element of the queue.
queue::push(g)  Adds the element ‘g’ at the end of the queue.
queue::pop()  Deletes the first element of the queue.

Recent Articles on C++ Queue 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!