The C++ Queue pop method is utilized to eliminate the element at the front of the queue. This operation is specifically designed for the removal of elements from the queue.
Syntax
void pop()
Parameters
The function solely executes the deletion task without requiring any input parameters.
Return value
There is an absence of a return value in this function; its purpose is implied solely for the removal of elements.
Example 1
#include <iostream>
#include <queue>
int main()
{
std::queue<int> newqueue;
int qint;
std::cout << "Enter some valid integer values(press 0 to end)";
do
{
std::cin>> qint;
newqueue.push(qint);
} while (qint);
std::cout << "newqueue contains: ";
while(!newqueue.empty())
{
std::cout <<" " <<newqueue.front();
newqueue.pop();
}
return 0;
}
Output:
Enter some valid integer values(press 0 to end)
1
3
4
5
6
7
0
newqueue contains: 1 3 4 5 6 7 0
Example 2
#include <iostream>
#include <queue>
using namespace std;
int main()
{
{
int a=0;
queue<int> newqueue;
newqueue.push(4);
newqueue.push(8);
newqueue.push(12);
newqueue.push(16);
while(!newqueue.empty())
{
newqueue.pop();
a++;
}
cout<<a;
}
}
Output:
Complexity
The complexity of the function is constant.
Data races
This function alters the container and all its components. Following the removal of an element from the queue, the positions of all remaining elements are adjusted accordingly.
Exception Safety
An assurance is given that the guarantee matches the operations carried out on the container object underneath.