queue::pop() in C++
The pop() function of the queue container is used to remove an element from the front of the queue(the oldest element in the queue). This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the <queue> header file. The element is removed from the queue container and the size of the queue is decreased by 1.
Syntax
queue_name.pop()
Parameters
- No parameters are passed
Result
- Removes the oldest element in the queue or basically the front element.
Examples:
Input : myqueue = 1, 2, 3
myqueue.pop();
Output : 2, 3
Input : myqueue = 3, 2, 1
myqueue.pop();
Output : 2, 1
Errors and Exceptions
- Shows an error if a parameter is passed.
- Shows no exception throw guarantee if the parameter doesn’t throw any exception.
Example of queue::pop()
// CPP program to illustrate
// Implementation of pop() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Empty Queue
queue<int> myqueue;
myqueue.push(0);
myqueue.push(1);
myqueue.push(2);
// queue becomes 0, 1, 2
myqueue.pop();
myqueue.pop();
// queue becomes 2
// Printing content of queue
while (!myqueue.empty()) {
cout << ' ' << myqueue.front();
myqueue.pop();
}
}
Output
2
Time Complexity: O(1) (Queue pop() operation take constant time complexity.)
Note: Here, output is printed on the basis of FIFO property.
queue push() and pop() in C++ STL
The queue is a type of container that operates in a First In First Out (FIFO) type of arrangement. Elements are inserted at the back(end) and are deleted from the front of the queue. For this purpose, the member functions queue::push() and queue::pop() are used respectively.
In this article, we will discuss these queue::pop() and queue()::push() functions.