A priority queue is a dynamically resizing container adopter in the C++ Standard Library. Elements in a priority queue are arranged in non-decreasing order such that the first element is always the greatest element in the queue. The priority_queue::pop()
is a method available in the STL which removes the element on top (largest element) of the queue, effectively reducing its size by one.
Syntax:
priorityq_name.pop();
Parameter: The priority_queue::pop()
does not accept any parameter
Return value: none
Example of priority_queue::pop() method
#include <iostream>> #include <queue> using namespace std; void print_q(priority_queue <int> q) //helper function to print the contents of a priority queue { priority_queue <int> temp = q; while (!temp.empty()) { cout<<temp.top()<<"\t"; temp.pop(); } cout<<endl; } int main () { priority_queue<int> q; q.push(10); q.push(20); q.push(40); q.push(30); cout<<"Before pop: \n"; print_q(q); q.pop(); cout<<"After pop: \n"; print_q(q); return 0; }
Output:
Before pop: 40 30 20 10 After pop: 30 20 10