C++ std::priority_queue::pop() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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
CallWhat 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

ParameterDescription
Nonepop() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingpop() returns void. It does not give back the removed element.
Undefined behaviorIf 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.

C++
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> pq;
    pq.push(5);
    pq.push(30);
    pq.push(10);

    int removed = pq.top();
    pq.pop();

    std::cout << "Removed: " << removed << '\n';
    std::cout << "New top: " << pq.top() << '\n';
    std::cout << "Size: " << pq.size() << '\n';
    return 0;
}

How It Works

  1. top() reads the largest value 30 and stores it in removed. This is needed because pop() gives nothing back.
  2. pop() takes 30 out of the queue. The next largest value, 10, moves to the top.
  3. The size drops from 3 to 2. The last two lines show the new top and the new size.

Example 2 — Pop Everything to Sort in Falling Order

Keep calling top() and pop() until the queue is empty. The numbers come out sorted from large to small.

C++
#include <iostream>
#include <queue>
#include <vector>

int main() {
    std::vector<int> data = {4, 1, 7, 3};
    std::priority_queue<int> pq(data.begin(), data.end());

    std::cout << "Falling order:";
    while (!pq.empty()) {
        std::cout << ' ' << pq.top();
        pq.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The queue is built from the vector with two iterators. All four numbers go inside in one step.
  2. Each round of the loop prints top() and then removes it with pop(), so the next-largest value becomes the top.
  3. This takes O(log n) per pop, so sorting the whole vector this way costs O(n log n). The loop ends when empty() is true.

Example 3 — Serve the Smallest Ticket First (Min-Heap)

With std::greater the smallest number is served first, like a numbered ticket line.

C++
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

int main() {
    std::priority_queue<int, std::vector<int>, std::greater<int>> tickets;
    tickets.push(12);
    tickets.push(5);
    tickets.push(9);

    while (!tickets.empty()) {
        std::cout << "Serving ticket " << tickets.top() << '\n';
        tickets.pop();
    }
    std::cout << "No more tickets.\n";
    return 0;
}

How It Works

  1. std::greater<int> turns the queue into a min-heap, so the smallest ticket number 5 is on top.
  2. The loop reads top() and then calls pop(). The queue never pops on empty, because the loop checks !tickets.empty() first.
  3. After pop() removes 12, empty() is true, the loop ends, and the program prints the last message.

Frequently Asked Questions

It removes the element with the highest priority (the top element) from the queue. It does not return the removed value.
No. pop() returns void. Read the value with top() first, and then call pop(), for example int x = pq.top(); pq.pop();.
It is undefined behavior. The program may crash. Check that !pq.empty() before you call pop().
No. You can only remove the top element with pop(). If you need to remove other elements, use a different container such as std::set or std::map.
Make a min-heap with std::greater: std::priority_queue<int, std::vector<int>, std::greater<int>> pq. Then pop() removes the smallest value first.

Next: push()

Add one element. The queue puts it in the right place by priority with std::priority_queue.

push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful