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 below example, the priority_queue::pop function is used to delete top element of the priority_queue called p_queue.
#include <iostream> #include <queue> using namespace std; int main (){ priority_queue<int> p_queue; //add new elements in the priority queue p_queue.push(100); p_queue.push(80); p_queue.push(990); p_queue.push(85); p_queue.push(10); cout<<"Popping out elements: "; while (!p_queue.empty()) { cout<<p_queue.top()<<" "; p_queue.pop(); } return 0; }
The output of the above code will be:
Popping out elements: 990 100 85 80 10
❮ C++ <priority_queue> Library