list::splice in C++ with examples

Here we will learn about the function list::splice in C++, which is used to transfer elements from one list to another. list::splice is an inbuilt function in the Standard temporary library. We use it in three ways:

    1. To transfer all the elements of a list into another list in C++

      Syntax:

      list1.splice (iterator position, list2)

      where iterator position is the position within list 1 where elements are added. All the elements of list 2 are transferred to list 1 at the end of list 1.

      Code:

      #include <iostream>
      #include <list>
      
      using namespace std;
      
      int main()
      {
          // initializing lists 
          list<int> l1 = { 1, 2}; 
          list<int> l2 = { 3,4, 5 }; 
      
        
         // transfer all the elements of l2 to l1 
          l1.splice(l1.end(), l2);   
          
          cout << "list l1 after splice operation" << endl; 
          for (auto i : l1) 
              cout << i << " "; 
        
          return 0;
      }
      
      Output: 1 2 3 4 5
    2. To transfer a single element from one list to another in C++

      Syntax:

      list1.splice (iterator position, list2, iterator j)

      where j specifies the iterator position of an element in list2 which is to be transferred. The element at the ith position in list 2 is transferred to list 1.
      Code:

      #include <iostream>
      #include <list>
      
      using namespace std;
      
      int main()
      {
          // initializing lists 
          list<int> l1 = { 1, 2}; 
          list<int> l2 = { 3,4, 5 }; 
          list<int>::iterator it; 
          it=l2.begin();
          // transfer the starting element of l2 to l1
          l1.splice(l1.end(), l2, it);
        
          
          cout << "list l1 after splice operation" << endl; 
          for (auto i : l1) 
              cout << i << " "; 
        
      
          return 0;
      }
      Output: 1 2 3 
      
      
    3. Transfer a range of elements

      Syntax:

      list1.splice (iterator position, list2, iterator first, iterator last)

      where first and last are iterators that specify the range of elements in l2 to be transferred to l1 and all other parameters are the same as discussed above.
      Code:

      #include <iostream>
      #include <list>
      
      using namespace std;
      
      int main()
      {
          // initializing lists 
          list<int> l1 = { 1, 2}; 
          list<int> l2 = { 3,4, 5,6,7 }; 
          list<int>::iterator it; 
          it=l2.begin();
          
          advance(it, 2); //advance iterator by 2 positions
        
          // transfer of elements from 3rd element to last in l2 at the end of l1
          l1.splice(l1.end(), l2, it, l2.end()); 
        
        
          
          cout << "list l1 after splice operation" << endl; 
          for (auto i : l1) 
              cout << i << " "; 
        
      
          return 0;
      }
      Output: 1 2 5 6 7

Leave a Reply

Your email address will not be published. Required fields are marked *