list splice() function in C++ STL


In this article we will be discussing the working, syntax and examples of list::splice() function in C++ STL.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is the list::splice()?

list::splice() is an inbuilt function in C++ STL which is declared in <list> header file. splice() is used to transfer the elements from one list container to another list container at a certain position. This function inserts the content to a list and removes those content from the other list modifying the size of the both lists.

Syntax

This function’s syntax can be defined as 3 ways:
list1.splice(position, list2);
list1.splice(position, list2, i);
list1.splice(position, list2, first, last);

Parameters

  • position − The position of the list where we want elements to be transferred.

  • list2 − The list from where we want to transfer the elements.

  • i: It is an iterator which specifies the position of the list2 from where the element we want to transfer till the end of the list2.

  • first, last − These both are the iterators which define the starting and ending positions of the list2 from which we want to transfer the elements.

Example

Input: list<int> List_container= { 10, 11, 13, 15};
      list<int> List2 = {1, 2, 3, 4};
      List_container.splice(2, List2);
Output:
      List_container= 10 11 13 15 3 4
      List2 = 1 2

Return Value

This function returns a reverse iterator pointing to the last element of the list. Reverse iterator is an iterator which moves in backward direction.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   list<int> myList_1 = { 10, 20, 30, 40 };
   list<int> myList_2 = { 50, 60 };
   list<int>::iterator i;
   i = myList_2.begin();
   myList_1.splice(myList_1.end(), myList_2, i);
      cout<<"list after splice operation" << endl;
   for (auto temp : myList_1)
      cout << temp << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

list after splice operation
10 20 30 40 50

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   list<int> myList_1 = { 10, 20};
   list<int> myList_2 = { 30, 40, 50};
   list<int> myList_3 = {60};
   myList_1.splice(myList_1.begin(), myList_2);
      cout << "list 1 after splice operation" << endl;
   for (auto x : myList_1)
      cout << x << " ";
   myList_3.splice(myList_3.end(), myList_1);
      cout << "\nlist 3 after splice operation" << endl;
   for (auto x : myList_3)
      cout << x << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

list 1 after splice operation
30 40 50 10 20
list 1 after splice operation
60 30 40 50 10 20

Updated on: 06-Mar-2020

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements