In C++, std::list::splice()moves nodes from another list into this list, before a position that you give. The nodes are relinked, so no element is copied, moved in memory, or destroyed.
You can splice a whole list, a single element, or a range of elements. You can even splice inside the same list to reorder it. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call splice on the list that receives the nodes. The first argument is the position before which the nodes are placed.
C++
a.splice(pos, b); // all of b
a.splice(pos, b, it); // only the element at it
a.splice(pos, b, first, last); // the range [first, last)
Call
What it does
a.splice(pos, b)
Moves all elements of b before pos. b becomes empty.
a.splice(pos, b, it)
Moves only the single element that it points to, from b to before pos.
a.splice(pos, b, first, last)
Moves every element in [first, last) from b to before pos. See splice_range().
b may be the same list as a. Then splice() only reorders the nodes inside one list.
Library declarations
C++
void splice(const_iterator pos, list& other);
void splice(const_iterator pos, list& other, const_iterator it);
void splice(const_iterator pos, list& other,
const_iterator first, const_iterator last);
// each form also has an overload taking list&& other (since C++11)
Parameters
Parameter
Description
pos
Required. An iterator into this list. The moved nodes are placed before it. end() is allowed.
other
Required. The list to take nodes from. For the whole-list form it must not be the same list as this one.
it
Only for the one-element form. An iterator to the element of other to move.
first, last
Only for the range form. The range of other to move. last is not included. pos must not be inside this range.
Return Value
Return Value
Description
Nothing
splice() returns void. The nodes now belong to this list.
C++ Examples
Example 1 — splice() a Whole List
Append all elements of list b to the end of list a.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> a = {1, 2, 3};
std::list<int> b = {10, 20};
a.splice(a.end(), b);
for (int n : a) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "b has " << b.size() << " elements\n";
return 0;
}
Output
1 2 3 10 20
b has 0 elements
How It Works
a.end() means “after the last element”, so the new nodes are added at the end.
splice(a.end(), b) hooks the two nodes of b onto the tail of a. The values 10 and 20 are not copied.
List a now has five elements. List b has none, because its nodes were handed over.
Example 2 — splice() One Element
Take one node out of b and put it at the front of a.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> a = {1, 2, 3};
std::list<int> b = {7, 8, 9};
a.splice(a.begin(), b, std::next(b.begin()));
std::cout << "a:";
for (int n : a) std::cout << ' ' << n;
std::cout << "\nb:";
for (int n : b) std::cout << ' ' << n;
std::cout << '\n';
return 0;
}
Output
a: 8 1 2 3
b: 7 9
How It Works
std::next(b.begin()) is an iterator to the second element of b, which holds 8.
splice(a.begin(), b, it) cuts only that node out of b and links it before the first node of a.
List a becomes 8 1 2 3 and list b keeps the others, 7 9. The sizes change by one each.
Example 3 — splice() Inside the Same List
Use the same list as source and target to move the last element to the front.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5};
l.splice(l.begin(), l, std::prev(l.end()));
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
5 1 2 3 4
How It Works
std::prev(l.end()) is an iterator to the last element, which holds 5.
The source and the target are both l. splice() unlinks the node with 5 and links it again before l.begin().
The size is still five, because nothing was added or removed. Only the order changed, from 1 2 3 4 5 to 5 1 2 3 4.
Frequently Asked Questions
It moves elements from another list (or from the same list) into this list, before a position that you give. It relinks nodes, so nothing is copied.
insert() copies values into new nodes and leaves the source unchanged. splice() moves the existing nodes, so the source loses them and no copy is made.
Yes. Iterators and references to the moved elements stay valid, but they now refer to elements inside the target list.
Yes, with the one-element or range form, as long as pos is not inside the moved range. This is a quick way to reorder elements.
Their elements sit in arrays, so moving them means copying or moving each value. A list is made of separate nodes, so only a few links need to change.