C++ std::stack::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::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
CallWhat 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

ParameterDescription
rgRequired. 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 ValueDescription
Nothingpush_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.

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

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

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

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

How It Works

  1. src holds three numbers. push_range(src) pushes them in order, 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. Because pushes follow range order, the last value 2 sits on top.

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

The new elements land on top of the old ones. The last new value becomes the top.

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

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

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

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

How It Works

  1. The stack starts with one element, 10, so it is the top at first.
  2. push_range(more) adds 25, 5, and 15 in that order. A std::array works as a range just like a vector does.
  3. 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;
}

How It Works

  1. Stack a uses push_range(). It is the shortest way, but it needs a C++23 compiler.
  2. Stack b uses a range-based for loop with push(). It works in every C++ version and is the easiest fallback.
  3. 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.

Next: size()

Count how many elements are in the stack with std::stack.

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