std::forwar...splice_after(3)
π std::forwar...splice_after(3) π
okay bestie, welcome to the C++ Standard Libary page for std::forward_list::splice_after β yes, the manual name is literally βC++ Standard Libary,β typo and all, we donβt fix icons here. This is the friend-group move operation for singly linked lists: it relocates nodes from one forward_list into another without copying anything. No cap. β¨
πΈ NAME πΈ
std::forward_list::splice_after β std::forward_list::splice_after
β¨ Synopsis β¨
Six overloads, all (since C++11) β here they are in their exact natural habitat:
void splice_after( const_iterator pos, forward_list& other ); (1)
(since C++11)
void splice_after( const_iterator pos, forward_list&& other ); (2)
(since C++11)
void splice_after( const_iterator pos, forward_list& other, (3)
(since C++11)
const_iterator it );
void splice_after( const_iterator pos, forward_list&& other, (4)
(since C++11)
const_iterator it );
void splice_after( const_iterator pos, forward_list& other, (5)
(since C++11)
const_iterator first, const_iterator last );
void splice_after( const_iterator pos, forward_list&& other, (6)
(since C++11)
const_iterator first, const_iterator last );
Moves elements from another forward_list to *this. The elements are inserted after the element pointed to by pos.
No elements are copied. No iterators or references become invalidated. The iterators to the moved elements now refer into *this, not into other.
(1),(2)β Moves all elements fromotherinto*this. The containerotherbecomes empty after the operation.(3),(4)β Moves the element pointed to by the iterator followingitfromotherinto*this. Has no effect ifpos == itor ifpos == ++it.(5),(6)β Moves the elements in the range(first, last)fromotherinto*this. The element pointed-to byfirstis not moved.
π¨ hard warning, bestie: the behavior is undefined if any of these are true. This is not a vibe, this is UB: -get_allocator() != other.get_allocator(), -posis neitherbefore_begin()nor a dereferenceable iterator in[begin(), end()), - for overloads(1,2),*thisandotherrefer to the same object, - for overloads(3,4), the iterator followingitis not a dereferenceable iterator intoother, or - for overloads(5,6), -(first, last)is not a valid range inother, - some iterators in(first, last)are not dereferenceable, or -posis in(first, last).
π Parameters π
posβ element after which the content will be insertedotherβ another container to move the content fromitβ iterator preceding the iterator to the element to move fromotherto*thisfirst,lastβ the range of elements to move fromotherto*this
π«Ά Return value π«Ά
(none)
π Exceptions π
Throws nothing. Iconic behavior, honestly.
π₯ Complexity π₯
(1,2)β Linear in the size ofother.(3,4)β Constant.(5,6)β Linear instd::distance(first, last).
π Example π
// Run this code
#include <cassert>
#include <forward_list>
int main()
{
using F = std::forward_list<int>;
// Demonstrate the meaning of open range (first, last)
// in overload (5): the first element of l1 is not moved.
F l1 = {1, 2, 3, 4, 5};
F l2 = {10, 11, 12};
l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend());
// Not equivalent to l2.splice_after(l2.cbegin(), l1);
// which is equivalent to
// l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end());
assert((l1 == F{1}));
assert((l2 == F{10, 2, 3, 4, 5, 11, 12}));
// Overload (1)
F x = {1, 2, 3, 4, 5};
F y = {10, 11, 12};
x.splice_after(x.cbegin(), y);
assert((x == F{1, 10, 11, 12, 2, 3, 4, 5}));
assert((y == F{}));
// Overload (3)
x = {1, 2, 3, 4, 5};
y = {10, 11, 12};
x.splice_after(x.cbegin(), y, y.cbegin());
assert((x == F{1, 11, 2, 3, 4, 5}));
assert((y == F{10, 12}));
// Overload (5)
x = {1, 2, 3, 4, 5};
y = {10, 11, 12};
x.splice_after(x.cbegin(), y, y.cbegin(), y.cend());
assert((x == F{1, 11, 12, 2, 3, 4, 5}));
assert((y == F{10}));
}
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2045β applied toC++11; behavior as published:O(1)splicing could not be guaranteed if theget_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)before_beginβ returns an iterator to the element before beginningcbefore_beginβ (public member function)
thatβs the whole page, bestie π source is http://cppreference.com, version date 2024.06.10, page std::forwar...splice_after(3) β mwah π
π¦ 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