Open In App

Queue in C++ STL

Last Updated : 03 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the data structure.

Example:

C++
#include <iostream>
#include <queue>
using namespace std;

int main() {
    
    // Creating a queue of integers
    queue<int> q;

    // Pushing elements into the queue
    q.push(3);
    q.push(4);
    q.push(5);

    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    return 0;
}

Output
3 4 5 

Explanation: In this program, we created a queue of integers and pushed three elements {3, 4, 5} into it.

Syntax

Queue is defined as the std::queue class template inside <queue> header file.

queue<T> q;

where,

  • T: Type of elements in the queue.
  • q: Name assigned to the queue.

Declaration and Initialization

In C++, queue can be declared and initialized in multiple ways as shown in the below example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // Declare empty queue
    queue<int> q1;
    
    // Insert some elements into queue
    q1.push(3);
    q1.push(4);
    q1.push(5);
    
    // Create another queue from q1
    queue<int> q2(q1);
    
    while (!q2.empty()) {
        cout << q2.front() << " ";
        q2.pop();
    }
    return 0;
}

Output
3 4 5 

Explanation: In the above program,

  • Statement queue<int> q1 creates an empty queue of integers. Elements are later added into it.
  • Statement queue<int> q2(q1) creates another queue q2 with same elements as q1.

Basic Operations

Here are the basic operations that can be performed on a queue:

1. Inserting Elements

New elements can only be inserted at back of the queue using push() function. The process of inserting elements in a queue is also called enqueue.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    queue<int> q;

    // Pushing elements into the queue
    q.push(3);
    q.push(4);
    q.push(5);
    
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    return 0;
}

Output
3 4 5 

2. Accessing Elements

Only the front and back elements of the queue can be accessed by using front() and back() functions respectively. We can’t access any middle element of queue.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    queue<int> q;
    q.push(3);
    q.push(4);
    q.push(5);

    // Accessing the front and back elements
    cout << q.front() << endl;
    cout << q.back();
    return 0;
}

Output
3
5

3. Deleting Elements

Elements can only be deleted from the front of the queue using the pop() function. This operation is also called dequeue.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    queue<int> q;
    q.push(3);
    q.push(4);
    q.push(5);
    
    // Deleting elements from front side
    // of the queue
    q.pop();
    
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    return 0;
}

Output
4
5

4. Pseudo Traversal

Since only the front and back element can be accessed in a queue, we cannot directly traverse it. On the other hand, we can create a copy of the queue, access the front element, and then delete it. By continuing this process until the copied queue is empty, we can effectively traverse all the elements of the queue.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    queue<int> q;
    q.push(3);
    q.push(4);
    q.push(5);
    
    // Create a copy
    queue<int> temp(q);
    
    while(!temp.empty()) {
        cout << temp.front() << " ";
        temp.pop();
    }
    return 0;
}

Output
3 4 5 

Time Complexity

The below table lists the time complexity of the basic operations on a queue:

OperationTime Complexity
Insert an elementO(1)
Delete an elementO(1)

Access front element

O(1)

Access back elementO(1)
Traverse the queueO(n)

Other Common Operations

The following are some more operations on queue that will help you become more familiarize with this container:

Internal Working of Queue

Queue container provides the built-in implementation of the Queue data structure in C++. It is a container adaptor that is built over another container like deque or list. The queue follows the FIFO (First In, First Out) principle to insert and delete elements. This behaviour is achieved using underlying data structures that allow constant time insert and delete operations at both ends.

Example, we cannot use vector as it does not allow constant time for deletion at both ends. But on the other hand, deque allows the constant time insertion at the back and deletion at the front. All the other operations of deque are restricted.

All Member Functions

Following is the list of all member functions of std::queue class in C++:

Functions

Description

front()

Access the front element of the queue.

back()

Access the end element of the queue.

empty()

Check whether a queue is empty or not.

size()

Returns the number of elements in the queue.

push()

Adding an element at the back of the queue.

push_range()

Adding multiple elements at the end of queue.

emplace()

Add the constructs element in top of the queue.

pop()

Delete the front element of the queue.

swap()

Swap two queues.


Practice Tags :

Similar Reads