In C++23, push_range() adds a whole range of elements at the back of a std::queue in a single call. A range can be a std::vector, a std::list, a plain array, or a view such as std::views::iota.
The elements are added in the order the range gives them, one behind the other, as if you had called push() for each value. Old elements stay in front, so the first element of the range leaves right after the old ones.
This function is a member function of std::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 q.push_range(...).
Syntax
Call push_range() with any range whose elements can be converted to the queue type. This function needs C++23.
C++
// compile with -std=c++23
std::queue<int> q;
std::vector<int> v = {20, 30, 40};
q.push(10);
q.push_range(v); // queue is now 10, 20, 30, 40
Call
What it does
q.push_range(rg)
Adds every element of the range rg at the back, in the order the range gives them.
Because the function only exists in C++23, older compilers will say that push_range is not a member of std::queue. In C++20 or earlier use a loop with push(). You can test for support with the macro __cpp_lib_containers_ranges.
Library declarations
C++
template<container-compatible-range<T> R>
void push_range(R&& rg); // since C++23
// queue<T, Container> from <queue>
// Appends the range to the underlying container,
// using c.append_range(rg) or ranges::copy(rg, back_inserter(c)).
Parameters
Parameter
Description
rg
Required. A range of elements that can be converted to the queue’s value type, such as a vector, an array, or a view. The range itself is not changed.
Return Value
Return Value
Description
Nothing
push_range() returns void. The size of the queue grows by the number of elements in the range.
C++ Examples
Example 1 — Add a Whole vector at Once
Push one number the old way, then add a vector of three numbers with one push_range() call.
viaLoop is built the classic way: a range-based for loop calls push() once for every name in the list.
viaRange gets the same three names with a single push_range(names) call. A std::list is a valid range, so no vector is needed.
Two queues of the same type can be compared with ==. It checks the elements in order, so the result is true, which proves both methods give the same queue.
Frequently Asked Questions
It adds all elements of a range at the back of the queue in one call, in the order the range gives them. It was added in C++23.
C++23. Compile with -std=c++23 (or /std:c++latest) and use a standard library with C++23 range support. Older versions will not find the function.
Any range whose elements convert to the queue type: a std::vector, std::list, std::array, a plain array, or a view such as std::views::iota.
At the back, behind all existing elements, and in the same order as the range. The old elements still leave first, so the queue stays FIFO.
Use a loop: for (const auto& x : range) q.push(x); This gives the same result and works in every standard.