In C++, pop()removes the top element of a std::priority_queue. That is the element with the highest priority, the largest one by default. After the call, the next-best element moves up to become the new top.
The important thing to remember is that pop()does not return the removed value. Its return type is void. If you need the value, read it with top() first, and then call pop().
This function is a member function of std::priority_queue<T>, declared in the <queue> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example pq.pop(...).
Syntax
Call pop() with empty brackets. First read the value with top() if you need it.
C++
std::priority_queue<int> pq;
pq.push(5);
pq.push(30);
pq.push(10);
int value = pq.top(); // read 30 first
pq.pop(); // then remove it
// the new top is 10
Call
What it does
pq.pop()
Removes the highest-priority element and restores the heap order. Returns nothing.
To take an element out, call top() first and then pop(). pop() takes O(log n) time. A priority queue has no way to remove an element from the middle, only the top one.
Library declarations
C++
void pop();
// priority_queue<T, Container, Compare> from <queue>
// It calls std::pop_heap(...) and then c.pop_back().
Parameters
Parameter
Description
None
pop() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
Nothing
pop() returns void. It does not give back the removed element.
Undefined behavior
If the queue is empty, there is nothing to remove. Check empty() first.
C++ Examples
Example 1 — Read with top(), Then pop()
Save the top value, remove it, and look at the new top.