How to Dequeue an Element from a Queue in C++?
Last Updated :
29 Feb, 2024
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++
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue< int > q;
for ( int i = 1; i <= 5; ++i) {
q.push(i);
}
q.pop();
while (!q.empty()) {
cout << q.front() << " " ;
q.pop();
}
cout << endl;
return 0;
}
|
Time Complexity: O(1)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...