C++ <forward_list> - splice_after() Function
The C++ forward_list::splice_after function is used to transfer elements from one forward_list to another. This function has following versions:
- The first version (1) transfers all elements from other into *this. The elements are inserted after the element pointed to by position. The container other becomes empty after the operation. The behavior is undefined if other refers to the same object as *this.
- The first version (2) transfers the element pointed to by it following it from other into *this. The element is inserted after the element pointed to by position.
- The first version (3) transfers the elements in the range (first, last) from other into *this. The elements are inserted after the element pointed to by position. The behavior is undefined if position is an iterator in the range (first,last).
Syntax
//entire list (1) void splice_after (const_iterator position, forward_list& other); void splice_after (const_iterator position, forward_list&& other); //single element (2) void splice_after (const_iterator position, forward_list& other, const_iterator it); void splice_after (const_iterator position, forward_list&& other, const_iterator it); //range of elements (3) void splice_after (const_iterator position, forward_list& other, const_iterator first, const_iterator last); void splice_after (const_iterator position, forward_list&& other, const_iterator first, const_iterator last);
Parameters
position |
Specify the position in the forward_list after which the elements of other are inserted. |
other |
Specify a forward_list object to transfer the content from. |
it |
Specify Iterator to an element in other preceding the one to be transferred. Only the single element that follows this is transferred. |
first |
Specify the starting position of Iterator in other. Transfers of the elements in the open interval range (first,last) to position. This open range includes all the elements between first and last, excluding first and last elements. |
last |
Specify the last position of Iterator in other. Transfers of the elements in the open interval range (first,last) to position. This open range includes all the elements between first and last, excluding first and last elements. |
Note: Member types iterator and const_iterator are forward iterator types that point to elements.
Return Value
None.
Time Complexity
Up to Linear i.e, Θ(n).
Example: transfer all elements of a forward_list
In the example below, the forward_list::splice_after function is used to transfer all elements from one forward_list to another.
#include <iostream> #include <forward_list> using namespace std; int main (){ forward_list<int> flist1{1, 2, 3, 4, 5}; forward_list<int> flist2{10, 20, 30, 40, 50}; forward_list<int>::iterator it; cout<<"flist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; //transfers all elements of flist2 into flist1 flist1.splice_after(flist1.begin(), flist2); cout<<"\n\nflist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
flist1 contains: 1 2 3 4 5 flist2 contains: 10 20 30 40 50 flist1 contains: 1 10 20 30 40 50 2 3 4 5 flist2 contains:
Example: transfer a single element of a forward_list
Consider the example below where this function is used to transfer a single element from one forward_list to another.
#include <iostream> #include <forward_list> using namespace std; int main (){ forward_list<int> flist1{1, 2, 3, 4, 5}; forward_list<int> flist2{10, 20, 30, 40, 50}; forward_list<int>::iterator it; cout<<"flist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; it = flist2.begin(); it++; //transfers a single element of flist2 into flist1 flist1.splice_after(flist1.begin(), flist2, it); cout<<"\n\nflist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
flist1 contains: 1 2 3 4 5 flist2 contains: 10 20 30 40 50 flist1 contains: 1 30 2 3 4 5 flist2 contains: 10 20 40 50
Example: transfer a range of elements of a forward_list
Consider one more example where this function is used to transfer a range of elements from one forward_list to another.
#include <iostream> #include <forward_list> using namespace std; int main (){ forward_list<int> flist1{1, 2, 3, 4, 5}; forward_list<int> flist2{10, 20, 30, 40, 50}; forward_list<int>::iterator it; cout<<"flist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; it = flist2.begin(); advance(it, 2); //transfers a single element of flist2 into flist1 flist1.splice_after(flist1.begin(), flist2, it, flist2.end()); cout<<"\n\nflist1 contains: "; for(it = flist1.begin(); it != flist1.end(); it++) cout<<*it<<" "; cout<<"\nflist2 contains: "; for(it = flist2.begin(); it != flist2.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
flist1 contains: 1 2 3 4 5 flist2 contains: 10 20 30 40 50 flist1 contains: 1 40 50 2 3 4 5 flist2 contains: 10 20 30
❮ C++ <forward_list> Library