queue::pop() method

Queues are an abstract data type, specifically designed to operate in a FIFO context (first-in-first-out). The elements are inserted into one end of the container and extracted from the other. They are a type of container adaptor in the C++ Standard Library. The queue::pop() is a method available in the STL which removes the oldest element present in the queue and effectively reduces the size by one.

Syntax:

queue_name.pop();

Parameters: The queue::pop() does not accept any parameters
Return value: none

Example of queue::pop() method

#include <iostream>
#include <queue>

using namespace std;

void print(queue<int> q)
{
     while (!q.empty())
  {
    cout<<q.front()<<"  ";
    q.pop();
  }
  cout<<endl;
}

int main ()
{
  queue<int> q1;

  for(int i = 1; i <= 5; i++)
    q1.push(i);

  cout<<"Before pop: \n";
  print(q1);

  q1.pop();
  cout<<"After pop: \n";
  print(q1);

  return 0;
}

Output:

Before pop:
1  2  3  4  5
After pop:
2  3  4  5

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top