Open In App

List splice() in C++ STL

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The list splice() is a built-in function in C++ STL used to transfer elements from one list to another. This function just reassigns the internal pointers of each node to the new position without copying or moving the data.

Let's take a look at a simple example that shows the how to use list splice() function:

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l1 = {1, 2, 5};
    list<int> l2 = {4, 3};

  	// Put entire list l2 at start of l1
    l1.splice(l1.begin(), l2);

    for (auto i : l1)
        cout << i << " ";
    return 0;
}

Output
4 3 1 2 5 

Explanation: All elements of list l2 is transfer to the beginning of the list l1.

This article covers the syntax of all implementations, common examples and FAQs of the list splice() function in C++.

Syntax of List splice()

The list splice() is a member function of std::list class defined inside <list> header file and have 3 implementations:

l1.splice(pos, l2); // For whole list
l1.splice(pos, l2, pos1); // For single element
l1.splice(pos, l2, first, last); // For range of elements.

Parameters:

  • pos: Iterator to the position where elements are to be inserted.
  • pos1: Iterator to the element to be transferred.
  • first: Iterator to the first element of range to be transferred.
  • last: Iterator to the element just after the last element of range.
  • l1: List where elements are to be transferred.
  • l2: List from where elements are to be transferred.

Return Value:

  • This function does not return any value.

Examples of List splice()

The following examples demonstrate the use of list splice() function for different purposes and in different scenarios:

Transfer All the Elements from One List to Another

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l1 = {1, 5, 8};
    list<int> l2 = {4, 6};

    // Transfer all elements from list
  	// l2 to list l1
    l1.splice(l1.begin(), l2);

    for (auto i : l1)
        cout << i << " ";
    return 0;
}

Output
4 6 1 5 8 

Explanation: We transferred all the elements of l2 at the beginning of the l1 by updating the pointer of the last node of the list l1.

Move the Position of an Element Within the List

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l = {1, 5, 8, 4, 6};

    // Move element 4 to the beginning of the list
    l.splice(l.begin(), l, next(l.begin(), 3));

    for (auto i : l)
        cout << i << " ";
    return 0;
}

Output
4 1 5 8 6 

Explanation: We have moved the element 4 from its original position to the beginning of the list l. This shows that we can also use the list splice() function to move the elements in the same list.

Transfer Single Element from One List to Another

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l1 = {1, 5, 8};
    list<int> l2 = {4, 6};

    // Transfer last element of l2 to l1
    l1.splice(l1.begin(), l2, l2.end());

    for (auto i : l1)
        cout << i << " ";
    return 0;
}

Output
3 1 2 5 8 

Explanation: Only the element at l2.end() is inserted at the beginning of the list l.

Transfer Range of Elements from One Position to Another in Same List

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l = {1, 5, 8, 4, 6};

    // Transfer element 4 and 6 at the beginning
    l.splice(l.begin(), l, next(l.begin(), 3),
             l.end());

    for (auto i : l)
        cout << i << " ";
    return 0;
}

Output
4 6 1 5 8 

Explanation: We transferred the last two elements 4 and 6 to the beginning of the list l.

Transfer Range of Element from One List to Another

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    list<int> l1 = {1, 5, 8};
    list<int> l2 = {4, 6};

    // Transfer range of elements from l2 to l1
    l1.splice(l1.end(), l2, l2.begin(),
              l2.end());

    for (auto i : l1)
        cout << i << " ";
    return 0;
}

Output
1 5 8 4 6 

Explanation: We transferred the first 2 elements from list l2 to end of the list l1.


Article Tags :
Practice Tags :

Similar Reads