list splice() function in C++ STL

The list::splice() is a built-in function in C++ STL which is used to transfer elements from one list to another. The splice() function can be used in three ways:

  1. Transfer all the elements of list x into another list at some position.
  2. Transfer only the element pointed by i from list x into the list at some position.
  3. Transfers the range [first, last) from list x into another list at some position.

Syntax:

list1_name.splice (iterator position, list2)
                or 
list1_name.splice (iterator position, list2, iterator i)
                or 
list1_name.splice (iterator position, list2, iterator first, iterator last)

Parameters: The function accepts four parameters which are specified as below:



  • position – Specifies the position where the elements are to be transfered.
  • list2 – It specifies a list object of the same type which is to be transfered.
  • i – It specifies an iterator to the position of an element in list2 which is to be transfered.
  • first, last – Iterators specifying a range of elements in list2 which is to be transferred in list1. The range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.

Return value: This function doesnot returns anything.

Below programs illustrate the above function:

Program 1: Transfer all the elements of the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// list::splice() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initializing lists
    list<int> l1 = { 1, 2, 3 };
    list<int> l2 = { 4, 5 };
    list<int> l3 = { 6, 7, 8 };
  
    // transfer all the elements of l2
    l1.splice(l1.begin(), l2);
  
    // at the beginning of l1
    cout << "list l1 after splice operation" << endl;
    for (auto x : l1)
        cout << x << " ";
  
    // transfer all the elements of l1
    l3.splice(l3.end(), l1);
  
    // at the end of l3
    cout << "\nlist l3 after splice operation" << endl;
    for (auto x : l3)
        cout << x << " ";
    return 0;
}

chevron_right


Output:

list l1 after splice operation
4 5 1 2 3 
list l3 after splice operation
4 5 1 2 3 6 7 8

Program 2: Transfer a single element.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// list::splice() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initializing lists and iterator
    list<int> l1 = { 1, 2, 3 };
    list<int> l2 = { 4, 5 };
    list<int>::iterator it;
  
    // Iterator pointing to 4
    it = l2.begin();
  
    // transfer 4 at the end of l1
    l1.splice(l1.end(), l2, it);
  
    cout << "list l1 after splice operation" << endl;
    for (auto x : l1)
        cout << x << " ";
    return 0;
}

chevron_right


Output:

list l1 after splice operation
1 2 3 4

Program 3: Transfer a range of elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate the
// list::splice() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initializing lists and iterator
    list<int> l1 = { 1, 2, 3, 4, 5 };
    list<int> l2 = { 6, 7, 8 };
    list<int>::iterator it;
  
    // iterator pointing to 1
    it = l1.begin();
  
    // advance the iterator by 2 positions
    advance(it, 2);
  
    // transfer 3, 4 and 5 at the
    // beginning of l2
    l2.splice(l2.begin(), l2, it, l1.end());
  
    cout << "list l2 after splice operation" << endl;
    for (auto x : l2)
        cout << x << " ";
    return 0;
}

chevron_right


Output:

list l2 after splice operation
3 4 5 6 7 8


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohataraghav12



Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.