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:
- Transfer all the elements of list x into another list at some position.
- Transfer only the element pointed by i from list x into the list at some position.
- Transfers the range [first, last) from list x into another list at some position.
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.
list l1 after splice operation 4 5 1 2 3 list l3 after splice operation 6 7 8 4 5 1 2 3
Program 2: Transfer a single element.
list l1 after splice operation 1 2 3 4
Program 3: Transfer a range of elements.
list l2 after splice operation 3 4 5 6 7 8