C++ Standard Library C++ STL Library

C++ priority_queue - pop() Function



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

priority_queue is similar to a heap, where elements can be inserted in any order and max heap element is retrieved or deleted first. Using this function effectively calls pop_heap to keep the heap property of the priority_queue and then calls pop_back of the underlying container to delete the element.

Syntax

void pop();
void pop();

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Logarithmic i.e, Θ(log(n)).

Example:

In the example below, the priority_queue::pop function is used to delete top element of the priority_queue called pqueue.

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

  //add new elements in the priority queue
  pqueue.push(100);
  pqueue.push(80);
  pqueue.push(990);
  pqueue.push(85);
  pqueue.push(10);

  cout<<"Popping out elements: ";
  while (!pqueue.empty()) {
    cout<<pqueue.top()<<" ";
    pqueue.pop();
  }
  return 0;
}

The output of the above code will be:

Popping out elements: 990 100 85 80 10 

❮ C++ <queue> Library