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

  1. Shows an error if a parameter is passed.
  2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

Example of queue::pop()

CPP
// 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.

Similar Reads

queue::push() in C++

The push() function of the queue container is used to insert an element at the back of the queue. This is an inbuilt function from C++ Standard Template Library(STL). This function belongs to the header file. The element is added to the queue container and the size of the queue is increased by 1....

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 header file. The element is removed from the queue container and the size of the queue is decreased by 1....

Application of push() and pop() in C++

Given a number of integers, add them to the queue and find the size of the queue without using the size function....

Difference between queue push() and queue pop() in C++

The following table list the differences between queue::push() and queue::pop():...