C++ Standard Library C++ STL Library

C++ queue - pop() Function



The C++ queue::pop function is used to delete the first element of the queue. Every deletion of element results into reducing the container size by one unless the queue is empty.

Syntax

void pop();
void pop();

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the queue::pop function is used to delete first elements of the queue called MyQueue.

#include <iostream>
#include <queue>
using namespace std;
 
int main (){
  queue<int> MyQueue;

  MyQueue.push(10);
  MyQueue.push(20);
  MyQueue.push(30);
  MyQueue.push(40);
  MyQueue.push(50);

  //deletes first element of the queue
  MyQueue.pop();
  //deletes next first element of the queue
  MyQueue.pop();

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

The output of the above code will be:

MyQueue contains: 30 40 50

❮ C++ <queue> Library

5