C++ std::list insert_range() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::insert_range() inserts every element of a range into a std::list, right before the position you give. The range can be a vector, an array, a deque, or a lazy view.

It was added in C++23. It is the range version of insert(pos, first, last): you pass one range instead of two iterators. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call insert_range with an iterator and one range. Compile with C++23, for example -std=c++23.

C++
auto it = l.insert_range(pos, v);                // v is a vector
l.insert_range(l.end(), std::views::iota(3, 6)); // adds 3 4 5
CallWhat it does
l.insert_range(pos, rg)Inserts every element of rg before pos and returns an iterator to the first new element.
l.insert_range(l.end(), rg)Adds the range at the very end of the list.

If your compiler is older than C++23, use insert(pos, first, last). For adding at the end only, C++23 also offers append_range().

Library declarations
C++
template<container-compatible-range<T> R>
iterator insert_range(const_iterator pos, R&& rg);   // since C++23

Parameters

ParameterDescription
posRequired. An iterator into this list. The new elements go before it. l.end() is allowed.
rgRequired. A range whose elements can be converted to T. It must not refer to this same list.

Return Value

Return ValueDescription
iteratorAn iterator to the first inserted element. If the range is empty, it returns pos.

C++ Examples

Example 1 — insert_range() from a Vector

Insert two vector values between two list values. Compile with -std=c++23.

C++
#include <iostream>
#include <iterator>
#include <list>
#include <vector>

int main() {
    std::vector<int> v = {20, 30};
    std::list<int> l = {10, 40};

    l.insert_range(std::next(l.begin()), v);

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::next(l.begin()) is an iterator to 40, the second node.
  2. insert_range() links one new node for 20 and one for 30 before that position.
  3. The vector v is unchanged. Its values were copied into the list.

Example 2 — insert_range() Returns the First New Element

Add generated numbers at the end and use the returned iterator to find where they start.

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

int main() {
    std::list<int> l = {1, 2};

    auto it = l.insert_range(l.end(), std::views::iota(3, 6));

    std::cout << "first new: " << *it << '\n';
    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::views::iota(3, 6) produces 3 4 5 without storing them anywhere.
  2. Passing l.end() as the position adds the new nodes after the last element.
  3. The returned iterator points at the first new node, so *it is 3, not the last element.

Example 3 — insert_range() with an Empty Range

When the range has no elements, nothing changes and the position comes back.

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

int main() {
    std::vector<int> none;
    std::list<int> l = {8, 9};

    auto it = l.insert_range(l.begin(), none);

    std::cout << std::boolalpha;
    std::cout << "returned begin(): " << (it == l.begin()) << '\n';
    std::cout << "size: " << l.size() << '\n';
    return 0;
}

How It Works

  1. The vector none has zero elements, so there is nothing to copy.
  2. When the range is empty, insert_range() returns the pos that you passed, here l.begin().
  3. The list still has two elements, 8 and 9. An empty insert is harmless.

Frequently Asked Questions

It is a C++23 member function that inserts all elements of a range before a given iterator. It is the range version of insert(pos, first, last).
C++23. Use a flag such as -std=c++23 and a compiler with C++23 library support. Older standards do not have it.
An iterator to the first inserted element, or pos if the range was empty.
insert(pos, first, last) takes two iterators. insert_range(pos, rg) takes one range object, which is shorter and also works with views such as std::views::iota.
No. insert(pos, first, last) does the same job and works in every C++ version. insert_range() just gives a cleaner call when you already have a range.

Next: max_size()

Get the largest number of elements a list could hold with std::list.

max_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.

7 people found this page helpful