C++ std::list::splice
The std::list::splice
function transfers elements from one std::list
to another. It does not copy or move the elements but directly manipulates the internal structure of the lists, making it a very efficient operation. The function offers flexibility by allowing you to transfer all elements, a single element, or a range of elements.
Syntax of std::list::splice
// 1. Splice entire list
void splice(const_iterator position, std::list<T>& x);
// 2. Splice single element
void splice(const_iterator position, std::list<T>& x, const_iterator i);
// 3. Splice range of elements
void splice(const_iterator position, std::list<T>& x, const_iterator first, const_iterator last);
Parameters
Parameter | Description |
---|---|
position | The position in the target list where elements will be inserted. |
x | The source list from which elements will be transferred. |
i | An iterator pointing to the single element in x to be transferred. |
first , last | Iterators defining the range of elements in x to be transferred. The range includes first but excludes last . |
Return Value
This function does not return a value. It modifies both the target and source lists by transferring elements between them.
Exceptions
The std::list::splice
function does not throw exceptions. It provides a no-throw guarantee because it operates by modifying internal pointers or data structures directly.
Examples for std::list::splice
Example 1: Splicing an Entire List
This example demonstrates how to use splice
to transfer all elements from one list to another:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {10, 20, 30};
std::list<int> list2 = {40, 50};
// Splice all elements from list2 into list1 at the beginning
list1.splice(list1.begin(), list2);
std::cout << "List 1 contents after splice: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after splice: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedlist1
is initialized with{10, 20, 30}
, andlist2
is initialized with{40, 50}
. - The
splice
function is called withlist1.begin()
andlist2
, transferring all elements fromlist2
to the beginning oflist1
. - After the operation,
list1
contains all elements, andlist2
is empty.
Output:
List 1 contents after splice: 40 50 10 20 30
List 2 contents after splice:
Example 2: Splicing a Single Element
This example demonstrates how to use splice
to transfer a single element from one list to another:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {10, 20, 30};
std::list<int> list2 = {40, 50};
// Splice the first element of list2 into list1 after the first element
list1.splice(++list1.begin(), list2, list2.begin());
std::cout << "List 1 contents after splice: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after splice: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
list1
is initialized with{10, 20, 30}
, andlist2
is initialized with{40, 50}
.- The
splice
function transfers the first element (40
) oflist2
tolist1
after its first element (10
). - After the operation,
list1
contains{10, 40, 20, 30}
, andlist2
contains{50}
.
Output:
List 1 contents after splice: 10 40 20 30
List 2 contents after splice: 50
Example 3: Splicing a Range of Elements
This example demonstrates how to use splice
to transfer a range of elements from one list to another:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 2, 3};
std::list<int> list2 = {4, 5, 6, 7};
// Splice a range (second and third elements) from list2 to list1 at the end
list1.splice(list1.end(), list2, ++list2.begin(), --list2.end());
std::cout << "List 1 contents after splice: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after splice: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
list1
is initialized with{1, 2, 3}
, andlist2
is initialized with{4, 5, 6, 7}
.- The range
{5, 6}
fromlist2
is transferred to the end oflist1
. - After the operation,
list1
contains{1, 2, 3, 5, 6}
, andlist2
contains{4, 7}
.
Output:
List 1 contents after splice: 1 2 3 5 6
List 2 contents after splice: 4 7