C++ std::priority_queue Functions

About This Index
This page is the C++ std::priority_queue Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a priority queue is on /cpp/priority-queue. Use this page when you need a specific function such as push(), top(), or pop().
std::priority_queue lives in the <queue> header. You call its functions with a dot, such as pq.push(5). It always keeps the highest-priority element on top (the largest value by default), and you can only see that top element. There is no iterator and no []. The 8 functions are listed below with the C++ version each one needs.
How to Call a priority_queue Function
Include <queue>, create a queue, and call the functions on it:
#include <queue>
#include <iostream>
#include <vector>
std::priority_queue<int> pq; // max-heap: largest on top
pq.push(10); // add
pq.push(40);
pq.emplace(20); // build in place (C++11)
int biggest = pq.top(); // 40, read only
std::size_t n = pq.size(); // 3
bool none = pq.empty(); // false
pq.pop(); // remove 40, returns nothing
std::vector<int> more = {5, 6};
pq.push_range(more); // C++23 only C++ std::priority_queue Functions Table
All function guides in this C++ tutorial series (8 tutorials), grouped by purpose.
Element access
1 functionLook at the one element you can see: the highest-priority element on top.
Capacity
2 functionsCheck if the queue is empty and how many elements it holds.
Modifiers
5 functionsAdd elements, remove the top element, or swap two queues.
| Function | Description | Since | Tutorial |
|---|---|---|---|
push() | Add one element. The queue puts it in the right place by priority. | C++98 | Try it |
push_range() | Add every element of a range at once (needs C++23). | C++23 | Try it |
emplace() | Build a new element inside the queue from constructor arguments. | C++11 | Try it |
pop() | Remove the highest-priority element. It does not return the value. | C++98 | Try it |
swap() | Swap the contents of two priority queues in one step. | C++11 | Try it |
Frequently Asked Questions
Start with emplace()
Build a new element right inside the queue, the first function in the alphabetical list.
12 people found this page helpful
