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

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

Definition

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
CallWhat 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

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

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

int main() {
    std::queue<int> q;
    q.push(10);

    std::vector<int> v = {20, 30, 40};
    q.push_range(v);

    std::cout << "Size: " << q.size() << '\n';
    std::cout << "Order out:";
    while (!q.empty()) {
        std::cout << ' ' << q.front();
        q.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The queue first holds 10. The vector v holds three more numbers, which are not yet in the queue.
  2. One push_range(v) call copies 20, 30, 40 behind the 10, in vector order. The size grows from 1 to 4.
  3. The loop shows the FIFO result: the old element 10 leaves first, then the range elements in the order they were stored.

Example 2 — Add a View with std::views::iota

A range does not have to be stored anywhere. A view can make numbers on demand, and the queue copies them in.

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

int main() {
    std::queue<int> q;
    q.push_range(std::views::iota(1, 6));

    std::cout << "Size: " << q.size() << '\n';
    std::cout << "Front: " << q.front() << ", Back: " << q.back() << '\n';

    std::cout << "Items:";
    while (!q.empty()) {
        std::cout << ' ' << q.front();
        q.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::views::iota(1, 6) is a lazy range that gives 1, 2, 3, 4, 5 and stops before 6. No vector is created.
  2. push_range() walks that view and pushes each number, so the queue holds five elements. front() is 1 and back() is 5.
  3. The drain loop prints the numbers in the range order. A new range always adds at the back, so the queue stays in FIFO order.

Example 3 — push_range() vs a Loop of push()

Both ways build the same queue. The C++23 call is shorter and works with any range, such as a std::list.

C++
#include <iostream>
#include <list>
#include <queue>
#include <string>

int main() {
    std::list<std::string> names = {"Mia", "Noah", "Zoe"};

    std::queue<std::string> viaLoop;
    for (const std::string& n : names) {
        viaLoop.push(n);
    }

    std::queue<std::string> viaRange;
    viaRange.push_range(names);

    std::cout << std::boolalpha;
    std::cout << "loop size: " << viaLoop.size()
              << ", range size: " << viaRange.size() << '\n';
    std::cout << "Queues equal: " << (viaLoop == viaRange) << '\n';
    return 0;
}

How It Works

  1. viaLoop is built the classic way: a range-based for loop calls push() once for every name in the list.
  2. 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.
  3. 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.

Next: size()

Count how many elements are waiting in the queue with std::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.

13 people found this page helpful