std::priority_queue<T> · C++

pop

Removes the top element: std::pop_heap then c.pop_back().

void pop();

Complexity

Worst caseO(log n) (at most 2 log₂ n comparisons)
Extra spaceO(1)

pop_heap swaps the top to the end and sifts the new root down.

Tested example

std::priority_queue<std::pair<int, std::string>> tasks;
tasks.emplace(2, "write tests");
tasks.emplace(9, "fix outage");
assert(tasks.top().second == "fix outage");
tasks.pop();  // returns nothing: read top() first
assert(tasks.top().first == 2);
// pop() or top() on an empty queue is undefined behaviour: check empty() first
Runs in the test suite on C++23, GCC 15.3 (libstdc++) · examples/cpp/collections/priority_queue_examples.cpp

Behaviour

Returns
Nothing.
Errors and preconditions
Undefined behaviour on an empty queue.
Changes
size() decreases by 1.
Memory and invalidation
No allocation.