In C++23, push_range() adds many elements at once to a std::stack. You give it a range, such as a std::vector, a std::array, or a std::list, and it pushes 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 last element of the range sits on top, because each value is pushed in order.
This function is a member function of std::stack<T>, declared in the <stack> header. It needs C++23 (-std=c++23) and a compiler with C++23 library support. You call it with a dot, for example st.push_range(...).
Syntax
Pass a range to push_range(). You need a C++23 compiler for this function.
C++
std::stack<int> st;
std::vector<int> src = {4, 8, 2};
st.push_range(src); // C++23
int newest = st.top(); // 2, last element of the range
Call
What it does
st.push_range(rg)
Pushes every element of the range rg onto the stack, in 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(). 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
// in order, so the last element of rg becomes the top.
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 stack. The source range is not changed.
Return Value
Return Value
Description
Nothing
push_range() returns void. The stack 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 stack starts with one element, 10, so it is the top at first.
push_range(more) adds 25, 5, and 15 in that order. A std::array works as a range just like a vector does.
The last value of the range, 15, becomes the new top. The size is 1 + 3 = 4.
Example 3 — Compare Three Ways to Fill a Stack
Before C++23 you can use a loop. All three ways shown here end with the same top and size.
C++
#include <iostream>
#include <stack>
#include <vector>
int main() {
std::vector<int> v = {3, 9, 1, 7};
std::stack<int> a;
a.push_range(v); // C++23
std::stack<int> b;
for (int n : v) {
b.push(n); // any C++ version
}
std::stack<int> c;
for (std::size_t i = 0; i < v.size(); ++i) {
c.push(v[i]);
}
std::cout << "push_range: top = " << a.top() << ", size = " << a.size() << '\n';
std::cout << "push loop: top = " << b.top() << ", size = " << b.size() << '\n';
std::cout << "index loop: top = " << c.top() << ", size = " << c.size() << '\n';
return 0;
}
Output
push_range: top = 7, size = 4
push loop: top = 7, size = 4
index loop: top = 7, size = 4
How It Works
Stack a uses push_range(). It is the shortest way, but it needs a C++23 compiler.
Stack b uses a range-based for loop with push(). It works in every C++ version and is the easiest fallback.
Stack c pushes by index. All three stacks end with top 7 and size 4, because the last value pushed was 7.
Frequently Asked Questions
It pushes every element of a range, such as a vector or array, onto the stack in one call. Elements are pushed in range order, so the last element becomes the top.
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. That works in every C++ version and gives the same LIFO order.
No. The source vector, array, or list is only read. The elements are copied (or moved from an rvalue range) into the stack.
No. It adds on top of what is already in the stack. If you want a fresh stack, create a new stack or swap with an empty one first.