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.
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
Parameter
Description
rg
Required. 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 Value
Description
Nothing
push_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.
The queue starts with one element, 10, so it is the top at first.
push_range(more) adds 25, 5, and 15. A std::array works as a range just like a vector does.
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;
}
Output
push_range: top = 9, size = 4
push loop: top = 9, size = 4
iterators: top = 9, size = 4
How It Works
Queue a uses push_range(). It is the shortest way, but it needs a C++23 compiler.
Queue b uses a range-based for loop with push(). It works in every C++ version and is the easiest fallback.
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.