C++ std::list splice_range() Function

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

Definition

This page covers the range form of std::list::splice(), written splice(pos, other, first, last). It moves every node in [first, last) from other into this list, before pos.

The standard has no member that is literally named splice_range. C++23 added assign_range, insert_range, append_range, and prepend_range, but a range of nodes is still moved with the four-argument splice(), which has worked since C++98. 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 splice with four arguments: the target position, the source list, and the first and last iterator of the range to move.

C++
a.splice(pos, b, first, last);       // move [first, last) from b
a.splice(a.end(), b, mid, b.end());  // move the tail of b to the end of a
CallWhat it does
a.splice(pos, b, first, last)Moves every element in [first, last) from b to before pos in a.
l.splice(pos, l, first, last)Same list as source and target. It moves a block of elements to a new place in the same list.

last is not moved. If first == last the range is empty and nothing happens.

Library declarations
C++
void splice(const_iterator pos, list& other,
            const_iterator first, const_iterator last);

void splice(const_iterator pos, list&& other,
            const_iterator first, const_iterator last);   // since C++11

Parameters

ParameterDescription
posRequired. An iterator into this list. The moved nodes are placed before it. If other is this list, pos must not lie inside [first, last).
otherRequired. The list that currently holds the range.
firstRequired. An iterator to the first element to move.
lastRequired. An iterator one place after the last element to move. It is not moved. other.end() is allowed.

Return Value

Return ValueDescription
NothingThe function returns void. The nodes of the range now belong to this list.

C++ Examples

Example 1 — Splice the Middle of a List

Move three elements from the middle of b into the middle of a.

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

int main() {
    std::list<int> a = {1, 2, 3};
    std::list<int> b = {10, 20, 30, 40, 50};

    a.splice(std::next(a.begin()), b,
             std::next(b.begin()), std::prev(b.end()));

    std::cout << "a:";
    for (int n : a) std::cout << ' ' << n;
    std::cout << "\nb:";
    for (int n : b) std::cout << ' ' << n;
    std::cout << '\n';
    return 0;
}

How It Works

  1. The range starts at std::next(b.begin()), which is 20, and ends at std::prev(b.end()), which is 50. The element 50 is not included.
  2. The target position std::next(a.begin()) is the node with 2, so the three moved nodes go between 1 and 2.
  3. List b keeps only its first and last element. The sizes are now six and two.

Example 2 — Splice a Block Inside the Same List

Use the same list as source and target to move the last two elements to the front.

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

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

    l.splice(l.begin(), l, std::next(l.begin(), 4), l.end());

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

How It Works

  1. std::next(l.begin(), 4) points at 5, and l.end() marks the end of the list. So the block is 5 6.
  2. The target l.begin() is outside that block, which is required. The block is cut out and linked before 1.
  3. The list still has six elements. Only the order changed, from 1 2 3 4 5 6 to 5 6 1 2 3 4.

Example 3 — Splice the Tail Found with std::find_if()

Search for the first element that is at least 30, then move it and everything after it to another list.

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

int main() {
    std::list<int> a = {1};
    std::list<int> b = {10, 20, 30, 40};

    auto mid = std::find_if(b.begin(), b.end(), [](int x) { return x >= 30; });
    a.splice(a.end(), b, mid, b.end());

    std::cout << "a:";
    for (int n : a) std::cout << ' ' << n;
    std::cout << "\nb:";
    for (int n : b) std::cout << ' ' << n;
    std::cout << '\n';
    return 0;
}

How It Works

  1. std::find_if() returns an iterator to the first element of b that passes the test. That is the node with 30.
  2. splice(a.end(), b, mid, b.end()) moves the nodes from mid up to the end of b to the end of a.
  3. List a becomes 1 30 40 and list b keeps the part before the match, 10 20. Nothing was copied.

Frequently Asked Questions

No. The range form is the four-argument splice(pos, other, first, last). C++23 added assign_range, insert_range, append_range, and prepend_range, but not a member called splice_range.
It moves all nodes in [first, last) from another list, or from the same list, to a position before pos. The nodes are relinked and nothing is copied.
No. The range is half open, like all C++ iterator ranges. first is moved, last is not.
Not if source and target are the same list. A pos inside [first, last) gives undefined behavior. Choose a position outside the block.
Use insert(pos, first, last), or insert_range() in C++23. They copy the values and leave the source list unchanged.

Next: swap()

Exchange the contents of two lists with std::list.

swap() 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.

14 people found this page helpful