C++ priority_queue

Beginner
8 min read
Updated: Nov 2026
3 examples
<queue>

Definition and Usage

In C++, std::priority_queue<T> is a container from the <queue> header that always keeps the most important element on top. You add elements in any order, and you always take out the one with the highest priority first.

By default the largest value has the highest priority, so the queue is a max-heap. The daily pattern is simple: push() to add, top() to read the best element, and pop() to remove it. Use empty() before you read or remove.

This page teaches what a priority queue is. The priority_queue Functions index lists every function guide in this series.

You can only see the top.

A priority queue has no [], no at(), and no begin() or end(). You cannot loop over all elements with iterators. Reading or removing from an empty queue with top() or pop() is undefined behavior, so check empty() first.

C++ Syntax

Common ways to create and use a std::priority_queue:

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

std::priority_queue<int> maxHeap;                                            // largest on top
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;    // smallest on top

maxHeap.push(10);
maxHeap.push(40);
maxHeap.emplace(20);                  // C++11

int biggest = maxHeap.top();          // 40
maxHeap.pop();                        // removes 40, returns nothing
std::size_t n = maxHeap.size();       // 2
bool none = maxHeap.empty();          // false

std::vector<int> v = {3, 1, 2};
std::priority_queue<int> fromVector(v.begin(), v.end());   // build from a range

Max-Heap vs Min-Heap

The only thing that changes is the comparison. Here is a quick comparison for beginners:

FeatureMax-heap (default)Min-heap
Typepriority_queue<int>priority_queue<int, vector<int>, greater<int>>
Comparison usedstd::lessstd::greater
What top() givesThe largest elementThe smallest element
Pop order for 4, 1, 77, 4, 11, 4, 7
Extra headers<queue><queue>, <vector>, <functional>
Typical useBiggest score, most urgent taskShortest distance, smallest ticket, top K largest

priority_queue vs queue

Both are container adaptors from <queue>. They share push(), pop(), size(), and empty(). The difference is the order in which elements come out:

Featurestd::queuestd::priority_queue
Order outOrder of arrival (FIFO)Highest priority first
Read the next itemfront() (and back())top() only
Cost of push()O(1)O(log n)
Cost of pop()O(1)O(log n)
Use it forWaiting lines, breadth-first searchTask scheduling, shortest path, top K values

Pick the function by the job you want to do:

FunctionUse it when you want to…
push() / emplace()Add one element. Use emplace() to build a pair or struct right inside the queue
push_range()Add a whole vector or array at once (needs C++23)
top() / pop()Read the best element, then remove it. pop() returns nothing
empty() / size()Check that the queue has elements before top() or pop(), or count them
swap()Exchange two queues of the same type, or empty a queue

C++ Examples

Three short programs that show how a std::priority_queue works.

Example 1 — Push, Top and Pop

A first program: push four numbers, read the biggest one, and pop them all out.

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

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

    std::cout << "size: " << pq.size() << '\n';
    std::cout << "top: " << pq.top() << '\n';

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

How It Works

  1. Four push() calls add 20, 5, 30, 10. The order of the pushes does not matter, because the queue sorts by priority inside.
  2. size() counts 4 elements. top() shows 30, the largest, and leaves it in the queue.
  3. The loop uses top() to read and pop() to remove, until empty() is true. The numbers come out from the largest to the smallest.

Example 2 — Max-Heap and Min-Heap Side by Side

The default queue gives the largest number first. Add std::greater to get the smallest number first.

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

int main() {
    std::priority_queue<int> maxHeap;
    std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;

    int values[] = {4, 1, 7};
    for (int v : values) {
        maxHeap.push(v);
        minHeap.push(v);
    }

    std::cout << "max-heap:";
    while (!maxHeap.empty()) {
        std::cout << ' ' << maxHeap.top();
        maxHeap.pop();
    }
    std::cout << '\n';

    std::cout << "min-heap:";
    while (!minHeap.empty()) {
        std::cout << ' ' << minHeap.top();
        minHeap.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Both queues get the same three numbers. The only difference is the type: the second queue adds the container std::vector<int> and the comparison std::greater<int>.
  2. The default std::less makes a max-heap, so the largest number 7 counts as the highest priority and leaves first.
  3. std::greater flips the rule, so the smallest number 1 counts as the highest priority. The min-heap prints 1 4 7.

Example 3 — queue vs priority_queue

A normal queue serves items in the order they arrived. A priority queue serves the most important item first.

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

int main() {
    std::queue<int> line;
    std::priority_queue<int> pq;

    int values[] = {3, 9, 1};
    for (int v : values) {
        line.push(v);
        pq.push(v);
    }

    std::cout << "queue order:          ";
    while (!line.empty()) {
        std::cout << line.front() << ' ';
        line.pop();
    }
    std::cout << '\n';

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

How It Works

  1. Both containers receive 3, 9, 1 in the same order, and both use push(), pop(), and empty().
  2. std::queue is first in, first out (FIFO). It reads with front() and gives the numbers back as they arrived: 3 9 1.
  3. std::priority_queue reads with top() and ignores the arrival order. It gives the largest first: 9 3 1.

Next: C++ priority_queue Functions Index

After you know what a priority queue is for, open the function index for a full tutorial on each function.

Start withWhy
push()Add elements, the first thing every queue needs
top()Read the highest-priority element without removing it
pop()Remove the top element (remember: it returns nothing)
emplace()Build pairs and structs right inside the queue

Full list: C++ std::priority_queue Functions index (/cpp/priority-queue/function).

Frequently Asked Questions

std::priority_queue is a container adaptor from the <queue> header. It keeps its elements in a heap, so the element with the highest priority is always on top. By default the largest value is on top, which is called a max-heap.
Add std::greater as the third template argument: std::priority_queue<int, std::vector<int>, std::greater<int>> pq. Then the smallest value is on top. Include <functional> and <vector>.
std::queue serves elements in the order they arrived (first in, first out) and reads with front(). std::priority_queue serves the highest-priority element first and reads with top(), no matter when it was added.
No. A priority_queue has no iterators and no operator[]. You can only see the top element. To visit every element, loop with while (!pq.empty()) and call top() then pop().
Open the C++ std::priority_queue Functions index. It lists all 8 member functions with a short description, the C++ version needed, and a full tutorial for each.

Browse every priority_queue function

Open the functions index for categorized guides with examples and FAQs.

Priority Queue Functions Index →

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.

8 people found this page helpful