In C++, the splice function is part of the STL list container and facilitates the efficient movement of an element from one list to another. Rather than duplicating or adding elements, the splice function alters the internal node pointers, allowing for swift operation execution.
In simpler terms, the C++ list splice method moves the elements from one list (referred to as list y) into another list at a designated location, resulting in changes to the sizes of both lists.
Syntax
It has the following syntax:
void splice(iterator pos, list& y);
void splice(iterator pos, list& y, iterator pos1);
void splice(iterator pos, list& y, iterator first, iterator last);
In this syntax,
- y: It is a list object of the same type from which the content is to be transferred.
- pos: It defines the position where the elements of y are inserted.
- pos1: The elemencpp tutorialed by the pos1 is to be transferred.
- (first, last): It defines the range of the elements that are to be transferred.
C++ List splice function Example
Let's consider a scenario to demonstrate the implementation of the splice method in C++.
Example
#include <iostream>
#include<list>
using namespace std; //using standard namespace
int main() //main function
{
list<int> li={1,2,3,4};
list<int> li1={5,6,7,8};
list<int>::iterator itr=li.begin();
li.splice(itr,li1);
for(list<int>::iterator itr=li.begin();itr!=li.end();++itr)
std::cout << *itr <<" ";
return 0;
}
Output:
5 6 7 8 1 2 3 4
Explanation:
In this illustration, we are working with two lists named li and li1, which are initialized with elements {1,2,3,4} and {5,6,7,8} respectively. Subsequently, the splice method is employed to move all elements from li1 to the beginning of li since the iterator itr references the initial element of li. Following the splice operation, li now holds {5,6,7,8,1,2,3,4} while li1 is left empty. Finally, the program exhibits the modified elements within li.
C++ Example to Splice a Single Element at a Specific Position
Let's consider a scenario to demonstrate the process of splitting a singular element at a designated location in the C++ programming language.
Example
#include <iostream>
#include<list>
using namespace std; //using standard namespace
int main() //main function
{
list<int> li={9,11,12,13};
list<int> li1={10,6,7,8};
list<int>::iterator itr=li.begin();
list<int>::iterator itr1=li1.begin();
++itr;
li.splice(itr,li1,itr1);
for(list<int>::iterator itr=li.begin();itr!=li.end();++itr)
std::cout << *itr <<" ";
return 0;
}
Output:
9 10 11 12 13
Explanation:
In this instance, we generate two lists named li and li1. Initially, we shift the iterator itr within list li to the second position and assign a new iterator itr1 to the initial element of li1. Subsequently, the splice function is employed to transfer the element referred to by itr1 (with a value of 10) from li1 to li just before the element indicated by itr (with a value of 11). As a result, li is transformed to {9, 10, 11, 12, 13} while li1 is updated to {6, 7, 8}.
C++ Example to transfer a Range of Elements Between Lists using the splice function
Let's consider a scenario to demonstrate the process of moving a set of items from one list to another by employing the splice function in the C++ programming syntax.
Example
#include <iostream>
#include<list>
using namespace std; //using standard namespace
int main() //main function
{
list<string> li={"programming language"};
list<string> li1={"C++","is","a","language"};
list<string>::iterator itr=li.begin();
list<string>::iterator itr1=li1.begin();
advance(itr1,3);
li.splice(itr,li1,li1.begin(),itr1);
for(list<string>::iterator itr=li.begin();itr!=li.end();++itr)
std::cout << *itr <<" ";
return 0;
}
Output:
C++ is a programming language
Explanation:
In this instance, we are working with two arrays. The first array, referred to as li, holds the term "programming language", while the second array, li1, includes the terms "C++", "is", and "a". By employing the splice method with a specified range, the initial three elements from li1 are moved to li just before the term "programming language". Consequently, the resulting array is "C++ is a programming language", with the word "language" remaining in li1.
Features of the List splice Function in C++
There are several features of the List splice function in C++. Some of them are as follows:
- In C++, it is used to transfer elements between lists without copying or moving elements.
- It provides support for moving the complete list, a single element, and a range.
- It also offers strong exception safety.
- It enables self-splicing inside the same list for reordering.
- It has a constant time complexity (O(1)) via pointer manipulation.
- It works only in the std::list function.
Conclusion
In summary, the splice method in C++ STL list is a crucial function enabling the transfer of elements within the same list without the need for element creation or destruction. It facilitates efficient manipulation by solely modifying node connections instead of duplicating data. Through various splice overloads, it is possible to relocate an entire list, an individual element, or a specific range of elements. This functionality of the splice method proves invaluable for dynamic tasks necessitating list reorganization or merging with a focus on optimizing performance.
C++ List splice function FAQs
The primary objective of the list::splice function in the C++ programming language is to transfer elements from one list to another list.
In C++, the list::splice method is frequently used to move elements from one list to another by rearranging nodes without duplicating or relocating data.
No, it is not possible to utilize the splice function with custom types or allocators in C++.
Yes, the splice method is compatible with any data type stored in std::list<T>, including user-defined classes. Since it simply reorganizes the connections between nodes, it eliminates the need for duplicating or transferring elements, resulting in efficient performance even with intricate data structures. It's important to note that custom allocators are only compatible when both lists share the same allocator type; otherwise, the outcome is unpredictable.
No, the splice function does not work with std::forward_list in C++.
Yes, the std::forwardlist (a singly linked list) features its unique spliceafter method instead of the splice function found in other list implementations. Due to the singly linked nature of forward_list, elements can exclusively be spliced after a specified position, contrasting with std::list which allows splicing both before and after a location. Nonetheless, the underlying mechanism remains alike, where nodes are rearranged without the need for element duplication or relocation.
When should we utilize the splice function instead of the insert method in C++?
In C++, the splice function is frequently used to transfer existing elements from one list to another, without duplicating them like the insert function does.
5) What are the various variations of the splice function overloading?
In C++, there are several types of overloads of the splice method. Some of them are as follows:
- It can move all elements from another list.
- It can also move a range of elements from another list.
- It can also move a single element from another list.