Open In App

How to Dequeue an Element from a Queue in C++?

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, 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. In this article, we will learn how to dequeue an element from a queue in C++ STL.

Example:

Input: 
Create a queue and enqueue the values {1, 2, 3, 4, 5}.

Output:
Queue after dequeue: {2, 3, 4, 5}, after dequeue the first element.

Dequeuing an Element from a Queue in C++

To dequeue an element from a queue in C++, we can use the std::queue::pop() function. This function deletes or dequeues the first element of the queue.

C++ Program to Dequeue an Element from a Queue

C++




// C++ Program to illustrate how to dequeue an element from
// a queue
#include <iostream>
#include <queue>
using namespace std;
  
// Driver Code
int main()
{
    // Creating a queue of integers
    queue<int> q;
    // Enqueueing elements into the queue
    for (int i = 1; i <= 5; ++i) {
        q.push(i);
    }
    // Dequeueing an element from the queue
    q.pop();
    // Displaying the queue elements
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
    return 0;
}


Output

2 3 4 5 

Time Complexity: O(1)
Space Complexity: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads