The list splice() is a built-in function in C++ STL used to transfer elements from one list to another. This function just reassigns the internal pointers of each node to the new position without copying or moving the data.
Let's take a look at a simple example that shows the how to use list splice() function:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l1 = {1, 2, 5};
list<int> l2 = {4, 3};
// Put entire list l2 at start of l1
l1.splice(l1.begin(), l2);
for (auto i : l1)
cout << i << " ";
return 0;
}
Explanation: All elements of list l2 is transfer to the beginning of the list l1.
This article covers the syntax of all implementations, common examples and FAQs of the list splice() function in C++.
Syntax of List splice()
The list splice() is a member function of std::list class defined inside <list> header file and have 3 implementations:
l1.splice(pos, l2); // For whole list
l1.splice(pos, l2, pos1); // For single element
l1.splice(pos, l2, first, last); // For range of elements.
Parameters:
- pos: Iterator to the position where elements are to be inserted.
- pos1: Iterator to the element to be transferred.
- first: Iterator to the first element of range to be transferred.
- last: Iterator to the element just after the last element of range.
- l1: List where elements are to be transferred.
- l2: List from where elements are to be transferred.
Return Value:
- This function does not return any value.
Examples of List splice()
The following examples demonstrate the use of list splice() function for different purposes and in different scenarios:
Transfer All the Elements from One List to Another
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l1 = {1, 5, 8};
list<int> l2 = {4, 6};
// Transfer all elements from list
// l2 to list l1
l1.splice(l1.begin(), l2);
for (auto i : l1)
cout << i << " ";
return 0;
}
Explanation: We transferred all the elements of l2 at the beginning of the l1 by updating the pointer of the last node of the list l1.
Move the Position of an Element Within the List
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l = {1, 5, 8, 4, 6};
// Move element 4 to the beginning of the list
l.splice(l.begin(), l, next(l.begin(), 3));
for (auto i : l)
cout << i << " ";
return 0;
}
Explanation: We have moved the element 4 from its original position to the beginning of the list l. This shows that we can also use the list splice() function to move the elements in the same list.
Transfer Single Element from One List to Another
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l1 = {1, 5, 8};
list<int> l2 = {4, 6};
// Transfer last element of l2 to l1
l1.splice(l1.begin(), l2, l2.end());
for (auto i : l1)
cout << i << " ";
return 0;
}
Explanation: Only the element at l2.end() is inserted at the beginning of the list l.
Transfer Range of Elements from One Position to Another in Same List
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l = {1, 5, 8, 4, 6};
// Transfer element 4 and 6 at the beginning
l.splice(l.begin(), l, next(l.begin(), 3),
l.end());
for (auto i : l)
cout << i << " ";
return 0;
}
Explanation: We transferred the last two elements 4 and 6 to the beginning of the list l.
Transfer Range of Element from One List to Another
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
list<int> l1 = {1, 5, 8};
list<int> l2 = {4, 6};
// Transfer range of elements from l2 to l1
l1.splice(l1.end(), l2, l2.begin(),
l2.end());
for (auto i : l1)
cout << i << " ";
return 0;
}
Explanation: We transferred the first 2 elements from list l2 to end of the list l1.
Similar Reads
Nested list in C++ STL list in STL is used to represent a linked list in C++. How to create a nested list. We are given n lists, we need to create a list of n lists. Examples: Input : Number of lists: 2 1st list: {1 2} 2nd list: {3 4 5 6} Output : [ [ 1 2 ] [ 3 4 5 6 ] ] Input : Number of lists: 3 1st list : {0 1} 2nd lis
2 min read
List in C++ STL In C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known.Example:C++#include <iostream
7 min read
list::operator= in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign n
2 min read
forward_list::splice_after() in C++ STL forward_list::splice_after() is an inbuilt function in CPP STL which transfers the elements in the range of first+1 to last from a given forward_list to another forward_list. The elements are inserted after the element pointed to by position in the parameter. Syntax: forwardlist1_name.splice_after(p
2 min read
list::swap() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::swap()This function is used to swap the con
2 min read
list size() function in C++ STL The list::size() is a built-in function in C++ STL that is used to find the number of elements present in a list container. That is, it is used to find the size of the list container.Syntax: list_name.size(); Time Complexity - Linear O(1) as per c++11 standard. Possible because the implementation ma
1 min read