C++ std::priority_queue Functions

Beginner
8 min read
Updated: Nov 2026
8 function guides
Function index

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().

Member functions of a container adaptor.

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:

C++
#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 function

Look at the one element you can see: the highest-priority element on top.

FunctionDescriptionSinceTutorial
top()Read the element with the highest priority without removing it.C++98Try it

Capacity

2 functions

Check if the queue is empty and how many elements it holds.

FunctionDescriptionSinceTutorial
empty()Check if the queue has no elements. It returns true or false.C++98Try it
size()Count how many elements are in the queue.C++98Try it

Modifiers

5 functions

Add elements, remove the top element, or swap two queues.

FunctionDescriptionSinceTutorial
push()Add one element. The queue puts it in the right place by priority.C++98Try it
push_range()Add every element of a range at once (needs C++23).C++23Try it
emplace()Build a new element inside the queue from constructor arguments.C++11Try it
pop()Remove the highest-priority element. It does not return the value.C++98Try it
swap()Swap the contents of two priority queues in one step.C++11Try it

Frequently Asked Questions

std::priority_queue has a small set of member functions: top() to read the highest-priority element, empty() and size() to check capacity, and push(), push_range() (C++23), emplace() (C++11), pop(), and swap() (C++11) to change the queue.
This series covers 8 functions, grouped as element access (top), capacity (empty, size), and modifiers (push, push_range, emplace, pop, swap).
emplace() and swap() need C++11. push_range() needs C++23. The functions top(), empty(), size(), push(), and pop() work in every C++ standard.
A priority_queue is a container adaptor that only lets you see the top element. It has no iterators and no random access. To visit every element you must use top() and pop() in a loop until empty() is true.
Read the C++ priority_queue page first. Then open push(), top(), and pop() from the tables below, because those three are used in almost every program that has a priority queue.

Start with emplace()

Build a new element right inside the queue, the first function in the alphabetical list.

emplace() 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.

12 people found this page helpful