list::splice() function in C++ STL

Get FREE domain for 1st year and build your brand new site

Free book on Binary Tree

We have explained the use of the function list::splice() in C++ STL along with the syntax, parameters, return type, time complexity and usage with different C++ code examples.

Table of content:

  1. Basics of C++ STL & list::splice()
  2. Key Features of list::splice()
  3. Syntax of list::splice()
  4. Parameters of list::splice()
  5. Explanation of list::splice() with example
  6. Return type of list::splice()
  7. Time complexity of list::splice()

We will dive into list::splice() now.

Basics of C++ STL & list::splice()

C++ STL(Standard Template Library) provides different sequence conatiner. One of them is list. List is a sequence container in which elements are stored in non-contiguous memory location.

One of the most important member function of list is splice() method.

Splice method is a built-in member function of list class. This method is used to transfer the content (some or all depending on parameters) of one list to another. It transfers the elements from list y into a list container at a specified position and this leads to the altering the sizes of both the list.

Key Features of list::splice()

  • It is a public member function of the list class. So it could be accessible from outside the class.
  • It transfer the content of one list to another.
  • This function doesn't return anything. So the return type is void.
  • The time complexity of this function varies form constant to linear depending upon the variant of splice method used.
  • This method alter the size of both the list . This point becomes more clear later in the examples.

Syntax of list::splice()

The splice function is basically used in three ways:

list1.splice (iterator position, list2);
                or
list1.splice (iterator position, list2, iterator i);
                or
list1.splice(iterator position,list2,iterator first,iterator second);

Parameters of list::splice()

This function accepts four parameters which are specified below:

  1. position: It is position within the container i.e. list1 where the elements of list2 are inserted.

  2. list2 - It is the list whose content has to be transferred to list1.

  3. i - Iterator to an element in list2 which is to be transferred to list1.

  4. first,last -They are iterators which specify the range of value to be transferred. All element between first and last in list2 including first but excluding last are transferred to list1.

Explanation of list::splice() with example

  1. syntax I :
list1.splice (iterator position, list2);

This above variant of splice method transfer all the content of list2 in list1 . The index in list1 from where list2 got added to list1 is specified by position parameter in the function call.

Code

#include <iostream>  
#include<list>  
using namespace std;  
int main()  
{  
    
  list<int> list1={10,20,30,40};  
  list<int> list2={5,6,7,8};  
  list<int>::iterator itr=list1.begin();  
  list1.splice(itr,list2);  
  cout<<"list1 after splice operation\n";
  for(list<int>::iterator itr=list1.begin();itr!=list1.end();++itr)  
  std::cout << *itr <<" ";  
  cout<<"\nprinting size of both list after splice method\n";
  cout<<"size of list1 "<<list1.size()<<endl;
  cout<<"size of list2 "<<list2.size()<<endl;
  return 0;  
} 

Output

list1 after splice operation
5 6 7 8 10 20 30 40 
printing size of both list after splice method
size of list1 8
size of list2 0

In the above code, entire list2 got transferred to list1 strarting form position list1.begin() i.e. from the beginning.

Hence list1 now consist of element from list1 and list2 as well which is shown by output of the code. Furthermore , if you try to print the size of both the list, you will see that the size of list1 is 8 and size of list2 is 0.

So the size of the both list changes after the splice operaion.All elements of list2 is transferred to list1 ,hence list1 becomes empty and its size become zero and the equal number of element get add to list2 ,hence its size increases to 8. That is what I mean by the fifth point of the key feature heading.

  1. Syntax II :
list1.splice (iterator position, list2, iterator i);

In the above version of splice function, a single element is transferred from list2 to list1 . The element which is transferred is the element pointed by iterator i. The position of this element in list1 is specified by the iterator position .

Code

#include <iostream> 
#include <list>
using namespace std; 
  
int main() 
{
    list<int> list1 = { 34,89,1000 }; 
    list<int> list2 = { 20,30 }; 
    list<int>::iterator it;
    it = list2.begin(); 
    list1.splice(list1.end(), list2, it); 
  
    cout << "list1 after splice operation" << endl; 
    for (auto x : list1) 
        cout << x << " ";
    cout<<"\nSize of both list after splice method call\n";
    cout<<"size of list1 "<<list1.size()<<endl;
    cout<<"size of list2 "<<list2.size()<<endl;
    return 0; 
}

Output

list1 after splice operation
34 89 1000 20 
Size of both list after splice method call
size of list1 4
size of list2 1

In the above code, only a single element of list2 is transferred to list1 . The element which is transferred is pointed by iterator list2.begin() i.e. first element. The position of this element in list1 is specified by position argument which is list1.end() i.e after the last element. Hence first element of list2 is appended to list1 which can be seen by the output as well. Also the size of both list changes after splice operation as explained in previous example.

  1. Syntax III :
list1.splice(iterator position,list2,iterator first,iterator second);

In this variant of the splice method, a range of value from list2 get transferred to list1. The element which are transferrred are in range of elememt pointed by iterator first and last .All element between first and last including first and excluding last is transferred . Their position in list1 is specified by position iterator .

Code

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> list1 = {10, 20};
   list<int> list2 = {30, 40, 50};

   list1.splice(list1.end(), list2, list2.begin(), list2.end());

   cout << "list1 after splice operation" << endl;

   for (auto it = list1.begin(); it != list1.end(); ++it)
      cout << *it << " ";
   cout<<"\nsize of both string after splice operation\n";
   cout<<"size of list1 "<<list1.size()<<endl;
   cout<<"size of list1 "<<list2.size();
   return 0;
}

Output

list1 after splice operation
10 20 30 40 50 
size of both string after splice operation
size of list1 5
size of list1 0

In the above code the element from range list2.begin() to list2.end() in list2 i.e all element is transferred to list1 . These position of these element in list1 is given as list1.end() i.e after the last element . Hence all the element of the list2 get added to the end of the list1.Also the size of both list changes after splice opertion as explained in first code .

Return type of list::splice()

This function does not return anything . It just perform some logical operation. Hence it's return type is void.

Time complexity of list::splice()

Different variant of his function has different time complexity .

  • for variant 1 and variant 2, time complexity is constant i.e. O(1).
  • for variant 3, time complexity is approximately linear i.e. O(n),where n is the size of the list2.

With this article at OpenGenus, you must have the complete idea of list::splice() in C++ STL. Enjoy.