std::list::splice(3)
β¨ NAME β¨
hey bestie π this is std::list::splice(3) from the C++ Standard Libary manual, and we're about to make list splicing make sense, fr fr. it's literally the std::list::splice function and it's giving pointer magic π
πΈ Synopsis πΈ
okay so here's the whole overload situation, exact vibes only:
void splice( const_iterator pos, list& other );
(1)
void splice( const_iterator pos, list&& other );
(2) (since C++11)
void splice( const_iterator pos, list& other, const_iterator it );
(3)
void splice( const_iterator pos, list&& other, const_iterator it
(4) (since C++11)
);
void splice( const_iterator pos, list& other,
(5)
const_iterator first, const_iterator last);
void splice( const_iterator pos, list&& other,
(6) (since C++11)
const_iterator first, const_iterator last );
okay so the actual tea β: this transfers elements from one list to another.
No elements are copied or moved, only the internal pointers of the list nodes are re-pointed. No iterators or references become invalidated, the iterators to moved elements remain valid, but now refer into *this, not into other. iykyk π
- 1,2) Transfers all elements from
otherinto*this. The elements are inserted before the element pointed to bypos. The containerotherbecomes empty after the operation. - 3,4) Transfers the element pointed to by
itfromotherinto*this. The element is inserted before the element pointed to bypos. - 5,6) Transfers the elements in the range
[first, last)fromotherinto*this. The elements are inserted before the element pointed to bypos.
β οΈ bestie, the behavior is undefined if: -get_allocator() != other.get_allocator(), - for overloads (1,2),*thisandotherrefer to the same object, - for overloads (3,4),itis not a dereferenceable iterator intoother, or - for overloads (5,6), -[first, last)is not a valid range inother, or -posis in[first, last).
π Parameters π
posβ element before which the content will be insertedotherβ another container to transfer the content fromitβ the element to transfer fromotherto*thisfirst, lastβ the range of elements to transfer fromotherto*this
π Return value π
(none) β like, nothing comes back, bestie. it's giving void energy β¨
π₯ Exceptions π₯
Throws nothing. not a single exception, periodt π
π Complexity π
- 1-4) Constant.
- 5,6) Constant if
otherrefers to the same object as*this, otherwise linear instd::distance(first, last).
π Example π
// Run this code
#include <iostream>
#include <list>
std::ostream& operator<<(std::ostream& ostr, const std::list<int>& list)
{
for (auto& i : list)
ostr << ' ' << i;
return ostr;
}
int main ()
{
std::list<int> list1{1, 2, 3, 4, 5};
std::list<int> list2{10, 20, 30, 40, 50};
auto it = list1.begin();
std::advance(it, 2);
list1.splice(it, list2);
std::cout << "list1:" << list1 << '\n';
std::cout << "list2:" << list2 << '\n';
list2.splice(list2.begin(), list1, it, list1.end());
std::cout << "list1:" << list1 << '\n';
std::cout << "list2:" << list2 << '\n';
}
β Output β
list1: 1 2 10 20 30 40 50 3 4 5
list2:
list1: 1 2 10 20 30 40 50
list2: 3 4 5
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 250β applied toC++98. Behavior as published: references and iterators to the moved element(s) were all invalidated. Correct behavior: they refer or point to the same element(s) in*this.N2525β applied toC++98. Behavior as published:O(1)splicing could not be guaranteed ifget_allocator() != other.get_allocator(). Correct behavior: behavior is undefined in this case.
π«Ά See also π«Ά
mergeβ merges two sorted lists (public member function)removeβ removes elements satisfying specific criteriaremove_ifβ (public member function)
okay that's the whole page bestie π footer vibes: source http://cppreference.com, version/date 2024.06.10, page std::list::splice(3) β and that's on periodt β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms