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

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

Definition

In C++23, push_range() adds many elements at once to a std::priority_queue. You give it a range, such as a std::vector, a std::array, or a std::list, and it inserts every element of that range.

It does the same job as a loop that calls push() for each element, but in one short line. After the call the heap order is correct, so top() returns the highest-priority element across the old and new values.

This function is a member function of std::priority_queue<T>, declared in the <queue> header. It needs C++23 (-std=c++23) and a compiler with C++23 library support. You call it with a dot, for example pq.push_range(...).

Syntax

Pass a range to push_range(). You need a C++23 compiler for this function.

C++
std::priority_queue<int> pq;
std::vector<int> src = {4, 8, 2};

pq.push_range(src);   // C++23

int biggest = pq.top();   // 8
CallWhat it does
pq.push_range(rg)Inserts every element of the range rg into the queue and restores the heap order.

C++23 only. Compile with -std=c++23 and use a compiler with C++23 library support. If the feature is missing, use a loop with push() or build the queue from two iterators. You can test for it with the macro __cpp_lib_containers_ranges.

Library declarations
C++
template<container-compatible-range<value_type> R>
void push_range(R&& rg);   // C++23

// Appends each element of rg to the underlying container
// and then calls std::make_heap(...) to restore the heap order.

Parameters

ParameterDescription
rgRequired. A range of elements, such as a vector, array, or list. Each element must be convertible to the type T of the queue. The source range is not changed.

Return Value

Return ValueDescription
Nothingpush_range() returns void. The queue size grows by the number of elements in the range.

C++ Examples

Example 1 — Push a Whole Vector (C++23)

Add three numbers from a vector in one call. Compile with -std=c++23.

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

int main() {
    std::vector<int> src = {4, 8, 2};
    std::priority_queue<int> pq;

    pq.push_range(src);   // C++23

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

How It Works

  1. src holds three numbers. push_range(src) inserts all of them into the queue in one call, so no loop is needed.
  2. The size becomes 3, which is the number of elements in the range. The vector src itself is unchanged.
  3. After the insert, the heap is in order, so top() gives the largest of the three values, 8.

Example 2 — Add a Range to a Queue That Already Has Items (C++23)

The new elements join the old ones. A new value can take over the top.

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

int main() {
    std::priority_queue<int> pq;
    pq.push(10);
    std::cout << "Top before: " << pq.top() << '\n';

    std::array<int, 3> more = {25, 5, 15};
    pq.push_range(more);   // C++23

    std::cout << "Top after: " << pq.top() << '\n';
    std::cout << "Size after: " << pq.size() << '\n';
    return 0;
}

How It Works

  1. The queue starts with one element, 10, so it is the top at first.
  2. push_range(more) adds 25, 5, and 15. A std::array works as a range just like a vector does.
  3. The value 25 is bigger than 10, so it becomes the new top. The size is 1 + 3 = 4.

Example 3 — Compare Three Ways to Fill a Queue

Before C++23 you can use a loop or the iterator constructor. All three ways give the same queue.

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

int main() {
    std::vector<int> v = {3, 9, 1, 7};

    std::priority_queue<int> a;
    a.push_range(v);                          // C++23

    std::priority_queue<int> b;
    for (int n : v) {
        b.push(n);                            // any C++ version
    }

    std::priority_queue<int> c(v.begin(), v.end());   // any C++ version

    std::cout << "push_range: top = " << a.top() << ", size = " << a.size() << '\n';
    std::cout << "push loop:  top = " << b.top() << ", size = " << b.size() << '\n';
    std::cout << "iterators:  top = " << c.top() << ", size = " << c.size() << '\n';
    return 0;
}

How It Works

  1. Queue a uses push_range(). It is the shortest way, but it needs a C++23 compiler.
  2. Queue b uses a range-based for loop with push(). It works in every C++ version and is the easiest fallback.
  3. Queue c is built straight from two iterators, but only while the queue is created. All three queues end up with top 9 and size 4.

Frequently Asked Questions

It inserts every element of a range, such as a vector or array, into the priority queue in one call. The heap order is restored, so top() still gives the highest-priority element.
push_range() was added in C++23. You need -std=c++23 and a compiler with C++23 library support. Older compilers give a compile error.
Use a loop that calls push() for each element, or build the queue from two iterators: std::priority_queue<int> pq(v.begin(), v.end()). Both work in older C++ versions.
No. The source vector, array, or list is only read. The elements are copied (or moved from an rvalue range) into the queue.
No. It adds to what is already in the queue. If you want a fresh queue, create a new priority_queue or swap with an empty one first.

Next: size()

Count how many elements are in the queue with std::priority_queue.

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

11 people found this page helpful