std::priority_queue<T> · C++
pop
Removes the top element: std::pop_heap then c.pop_back().
void pop();
Complexity
| Worst case | O(log n) (at most 2 log₂ n comparisons) |
|---|---|
| Extra space | O(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
Behaviour
- Returns
- Nothing.
- Errors and preconditions
- Undefined behaviour on an empty queue.
- Changes
- size() decreases by 1.
- Memory and invalidation
- No allocation.