STD::FORWARD_LIST(3cxx)
π std::forward_list< _Tp, _Alloc > β the manual, but make it make sense π
okay so bestie, you clicked on std::forward_list< _Tp, _Alloc >(3cxx) and honestly? iconic choice. this is the C++ standard library's singly linked list situation, and i'm gonna walk you through the whole thing like we're on facetime. no cap, every single detail is staying. let's go β¨
πΈ NAME πΈ
std::forward_list< _Tp, _Alloc > β a standard container with linear time access to elements, and fixed time insertion/deletion at any point in the sequence. that's the whole vibe. that's the assignment. π
β¨ SYNOPSIS β¨
#include <forward_list>
Inherits std::_Fwd_list_base< _Tp, _Alloc >. it's giving "in my inheritance era" π¦
π Public Types
okay so the type definitions, listen closely:
typedef _Alloc allocator_typetypedef _Base::const_iterator const_iteratortypedef _Alloc_traits::const_pointer const_pointertypedef const value_type & const_referencetypedef std::ptrdiff_t difference_typetypedef _Base::iterator iteratortypedef _Alloc_traits::pointer pointertypedef value_type & referencetypedef std::size_t size_typetypedef _Tp value_type
these are the little name badges every container wears, periodt.
π«Ά Public Member Functions
okay bestie this is the LONG part, grab a snack π
forward_list()=defaultβ creates a forward_list with no elements. empty. a blank canvas. main character energy before the character arc starts, yk?template<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> forward_list (_InputIterator __first, _InputIterator __last, const _Alloc &__al=_Alloc())β builds a forward_list from a range. take__firstto__last, voilΓ , you got a list π«forward_list (const _Alloc &__al) noexceptβ creates a forward_list with no elements. (same energy as the default one but you're picking your allocator, we love a choice).forward_list (const forward_list &__list)β the forward_list copy constructor. i'll have what she's having πforward_list (const forward_list &__list, const __type_identity_t< _Alloc > &__al)β copy constructor with allocator argument. copy but make it your own allocator, iykyk.forward_list (forward_list &&)=defaultβ the forward_list move constructor. she's moving out, no baggage π§³forward_list (forward_list &&__list, const __type_identity_t< _Alloc > &__al) noexcept(_Node_alloc_traits::_S_always_equal())β move constructor with allocator argument. moving but with your allocator picking the furniture.forward_list (size_type __n, const _Alloc &__al=_Alloc())β creates a forward_list with default constructed elements.__nof them. all fresh, all default, all cute.forward_list (size_type __n, const _Tp &__value, const _Alloc &__al=_Alloc())β creates a forward_list with copies of an exemplar element. "i'll take__ncopies of that one please" πforward_list (std::initializer_list< _Tp > __il, const _Alloc &__al=_Alloc())β builds a forward_list from an initializer_list.{1, 2, 3}bestie, we're so back.~forward_list () noexceptβ the forward_list dtor. she's logging off πtemplate<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> void assign (_InputIterator __first, _InputIterator __last)β assigns a range to a forward_list. out with the old, in with the range.void assign (size_type __n, const _Tp &__val)β assigns a given value to a forward_list.__ncopies of__val, slay.void assign (std::initializer_list< _Tp > __il)β assigns an initializer_list to a forward_list. brace yourself πconst_iterator before_begin () const noexceptβ the iterator that lives right beforebegin(). yes bestie, there's a seat before the first seat. that's the whole point of this container.iterator before_begin () noexceptβ same vibe, non-const edition.const_iterator begin () const noexceptβ start of the const party.iterator begin () noexceptβ start of the party.const_iterator cbefore_begin () const noexceptβ const "before the beginning" iterator. giving prequel energy.const_iterator cbegin () const noexceptβ const begin. πconst_iterator cend () const noexceptβ const end. the after-party that never happens.void clear () noexceptβ erases all the elements. ngl, brutal but necessary πtemplate<typename... _Args> iterator emplace_after (const_iterator __pos, _Args &&... __args)β constructs object in forward_list after the specified iterator. build it in place, right there, after__pos. so efficient, so iconic.template<typename... _Args> reference emplace_front (_Args &&... __args)β constructs object in forward_list at the front of the list. front row seats, built in place π₯bool empty () const noexceptβ is she empty? (no elements)const_iterator end () const noexceptβ end of the const party.iterator end () noexceptβ end of the party.iterator erase_after (const_iterator __pos)β removes the element pointed to by the iterator following pos. bye to the one right after__posπiterator erase_after (const_iterator __pos, const_iterator __last)β remove a range of elements. everything after__posup to__last, gone.reference front ()β the first element. main character of the list.const_reference front () constβ same but const.allocator_type get_allocator () const noexceptβ get a copy of the memory allocation object. your allocator, hand-delivered πtemplate<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>> iterator insert_after (const_iterator __pos, _InputIterator __first, _InputIterator __last)β inserts a range into the forward_list. after__pos, insert__firstthrough__last. we love a range moment.iterator insert_after (const_iterator __pos, _Tp &&__val)β insert one rvalue after__pos.iterator insert_after (const_iterator __pos, const _Tp &__val)β inserts given value into forward_list after specified iterator. that's the whole description, bestie, right there.iterator insert_after (const_iterator __pos, size_type __n, const _Tp &__val)β inserts a number of copies of given data into the forward_list.__ncopies of__val, after__pos. periodt.iterator insert_after (const_iterator __pos, std::initializer_list< _Tp > __il)β inserts the contents of an initializer_list into forward_list after the specified iterator. brace-init but make it after__pos.size_type max_size () const noexceptβ the maximum number of elements. the ceiling. the limit. the "how big can she get" number.void merge (forward_list &&__list)β merge sorted lists. β οΈ dangerous zone, bestie: merging is only safe if both lists are already sorted.template<typename _Comp > void merge (forward_list &&__list, _Comp __comp)β merge sorted lists according to comparison function. still sorted-only,__compdecides the order.void merge (forward_list &__list)β merge sorted lists. (lvalue reference flavor.)template<typename _Comp > void merge (forward_list &__list, _Comp __comp)β merge sorted lists according to comparison function. (lvalue reference flavor with__comp.)forward_list & operator= (const forward_list &__list)β the forward_list assignment operator. copy assignment, classic.forward_list & operator= (forward_list &&__list) noexcept(_Node_alloc_traits::_S_nothrow_move())β the forward_list move assignment operator. she's moving in, no cap.forward_list & operator= (std::initializer_list< _Tp > __il)β the forward_list initializer list assignment operator.=but with braces πvoid pop_front ()β removes first element. bye first element, you had a good run.void push_front (_Tp &&__val)β add data to the front of the forward_list. rvalue edition, front row.void push_front (const _Tp &__val)β add data to the front of the forward_list. lvalue edition, also front row.__remove_return_type remove (const _Tp &__val)β remove all elements equal to value. all of them. no survivors. (lovingly.)template<typename _Pred > __remove_return_type remove_if (_Pred __pred)β remove all elements satisfying a predicate. if__predsays yes, they're gone.template<typename _Pred > auto remove_if (_Pred __pred) -> __remove_return_typeβ the trailing-return-type twin of the above. same energy, different spelling.void resize (size_type __sz)β resizes the forward_list to the specified number of elements. glow-up or shrink,__szdecides.void resize (size_type __sz, const value_type &__val)β resizes the forward_list to the specified number of elements. (this time growing with copies of__val.)void reverse () noexceptβ reverse the elements in list. she's flipping the whole thing, no big deal πvoid sort ()β sort the elements of the list. put her in order.template<typename _Comp > void sort (_Comp __comp)β sort the forward_list using a comparison function.__compis the vibe-checker.void splice_after (const_iterator __pos, forward_list &&__list) noexceptβ insert contents of another forward_list. moving nodes around, no copies, we love to see it.void splice_after (const_iterator __pos, forward_list &&__list, const_iterator __i) noexceptβ insert element from another forward_list. just the one, after__pos.void splice_after (const_iterator __pos, forward_list &__list) noexceptβ splice the whole lvalue list after__pos.void splice_after (const_iterator __pos, forward_list &__list, const_iterator __i) noexceptβ splice one lvalue element after__pos.void swap (forward_list &__list) noexceptβ swaps data with another forward_list. trade places, bestie π__remove_return_type unique ()β remove consecutive duplicate elements. only consecutive ones, don't come for the distant twins.template<typename _BinPred > __remove_return_type unique (_BinPred __binary_pred)β remove consecutive elements satisfying a predicate.__binary_predis the judge.template<typename _BinPred > auto unique (_BinPred __binary_pred) -> __remove_return_typeβ trailing-return-type twin of that one too. consistent queen π
and then there's these two extra overloads hanging out at the bottom:
void splice_after (const_iterator __pos, forward_list &&, const_iterator __before, const_iterator __last) noexceptβ insert range from another forward_list. the range starts after__beforeand runs to__last.void splice_after (const_iterator __pos, forward_list &, const_iterator __before, const_iterator __last) noexceptβ insert range from another forward_list. same range energy, lvalue reference flavor.
π Detailed Description π
template<typename _Tp, typename _Alloc = allocator<_Tp>>
class std::forward_list< _Tp, _Alloc >
"A standard container with linear time access to elements, and fixed time insertion/deletion at any point in the sequence."
Since: C++11 β¨ (she's been here a minute and she's aged beautifully)
π Template Parameters
_Tpβ Type of element. the actual thing living in the list._Allocβ Allocator type, defaults toallocator<_Tp>. the memory manager, the backstage crew.
Meets the requirements of a container, a sequence, including the optional sequence requirements with the exception of at and operator[]. π¨ like, if you were expecting list[3], bestieβ¦ no. that's not this.
This is a singly linked list. Traversal up the list requires linear time, but adding and removing elements (or nodes) is done in constant time, regardless of where the change takes place. so getting somewhere is slow, but changing things once you're there? instant. we love a specialist π
Unlike std::vector and std::deque, random-access iterators are not provided, so subscripting ([]) access is not allowed. no shortcuts, no indexing, no list[2] fantasies. For algorithms which only need sequential access, this lack makes no difference. if you're walking the list anyway, you genuinely won't care, fr fr.
Also unlike the other standard containers, std::forward_list provides specialized algorithms unique to linked lists, such as splicing, sorting, and in-place reversal. she has her own little skillset and honestly? obsessed. π¦
okay bestie that's part 1, hydration check and then we keep going π§ more pages coming, don't go anywhere πΈ
π Constructor & Destructor Documentation π
okay bestie, this is the origin-story section β every single way to birth a forward_list, plus the way it says bye. there are a LOT of them and honestly each one ate. let's go π
πΈ forward_list () β the default, the blank canvas
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list () [default]
Creates a forward_list with no elements. That's it. Empty. A fresh lil baby list with nothing in it yet, truly main character energy before the glow-up β¨
πΈ forward_list (const _Alloc & __al) β empty, but make it allocator
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (const _Alloc & __al) [inline], [explicit], [noexcept]
Creates a forward_list with no elements β same emptiness as above, just with your own allocator in the mix.
Parameters:
__alβ an allocator object.
heads up: this one is marked[explicit], so no sneaky implicit conversions, and it's[noexcept]so she's chill about it π«Ά
πΈ forward_list (const forward_list& __list, const __type_identity_t<_Alloc>& __al) β copy, but with an allocator
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (const forward_list< _Tp, _Alloc > & __list, const __type_identity_t< _Alloc > & __al) [inline]
Copy constructor with allocator argument. π
Parameters:
__listβ input list to copy.__alβ an allocator object.
References std::forward_list< _Tp, _Alloc >::begin(), and std::forward_list< _Tp, _Alloc >::end().
πΈ forward_list (forward_list&& __list, const __type_identity_t<_Alloc>& __al) β move, with an allocator
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (forward_list< _Tp, _Alloc > && __list, const __type_identity_t< _Alloc > & __al) [inline], [noexcept]
Move constructor with allocator argument. grab the stuff, yeet the source, no cap π₯
Parameters:
__listβ input list to move.__alβ an allocator object.
πΈ forward_list (size_type __n, const _Alloc & __al = _Alloc()) β n default-constructed besties
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (size_type __n, const _Alloc & __al = _Alloc()) [inline], [explicit]
Creates a forward_list with default constructed elements.
Parameters:
__nβ the number of elements to initially create.__alβ an allocator object.
This constructor creates the forward_list with __n default constructed elements. so like, __n copies of "whatever the default is" β iconic mass production β¨
πΈ forward_list (size_type __n, const _Tp & __value, const _Alloc & __al = _Alloc()) β n copies of one vibe
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (size_type __n, const _Tp & __value, const _Alloc & __al = _Alloc()) [inline]
Creates a forward_list with copies of an exemplar element.
Parameters:
__nβ the number of elements to initially create.__valueβ an element to copy.__alβ an allocator object.
This constructor fills the forward_list with __n copies of __value. same fit, __n times π
πΈ forward_list (_InputIterator __first, _InputIterator __last, const _Alloc & __al = _Alloc()) β build from a range
template<typename _Tp , typename _Alloc = allocator<_Tp>> template<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>>
std::forward_list< _Tp, _Alloc >::forward_list (_InputIterator __first, _InputIterator __last, const _Alloc & __al = _Alloc()) [inline]
Builds a forward_list from a range. π¦
Parameters:
__firstβ an input iterator.__lastβ an input iterator.__alβ an allocator object.
Create a forward_list consisting of copies of the elements from [__first,__last). This is linear in N (where N is distance(__first,__last)).
note the fancy std::_RequireInputIter<_InputIterator> in the template β that's the constraint making sure you actually handed it input iterators, bestie. it's giving guard rails π«Ά
πΈ forward_list (const forward_list& __list) β the classic copy ctor
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (const forward_list< _Tp, _Alloc > & __list) [inline]
The forward_list copy constructor.
Parameters:
__listβ aforward_listof identical element and allocator types.
References std::forward_list< _Tp, _Alloc >::begin(), and std::forward_list< _Tp, _Alloc >::end().
πΈ forward_list (forward_list&&) β the classic move ctor
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (forward_list< _Tp, _Alloc > &&) [default]
The forward_list move constructor.
Parameters:
__listβ aforward_listof identical element and allocator types.
The newly-created forward_list contains the exact contents of the moved instance. The contents of the moved instance are a valid, but unspecified forward_list. β οΈ read that again β "valid, but unspecified" β so do NOT go poking at the moved-from list expecting anything specific. she's alive but she's not obligated to tell you what's in there π
πΈ forward_list (std::initializer_list<_Tp> __il, const _Alloc & __al = _Alloc()) β build from a braced list
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::forward_list (std::initializer_list< _Tp > __il, const _Alloc & __al = _Alloc()) [inline]
Builds a forward_list from an initializer_list. π
Parameters:
__ilβ aninitializer_listofvalue_type.__alβ an allocator object.
Create a forward_list consisting of copies of the elements in the initializer_list __il. This is linear in __il.size().
πΈ ~forward_list () β the destructor
template<typename _Tp , typename _Alloc = allocator<_Tp>>
std::forward_list< _Tp, _Alloc >::~forward_list () [inline], [noexcept]
The forward_list dtor. bye queen, thanks for the memories π
π Member Function Documentation π
β assign (_InputIterator __first, _InputIterator __last)
template<typename _Tp , typename _Alloc = allocator<_Tp>> template<typename _InputIterator , typename = std::_RequireInputIter<_InputIterator>>
void std::forward_list< _Tp, _Alloc >::assign (_InputIterator __first, _InputIterator __last) [inline]
Assigns a range to a forward_list.
Parameters:
__firstβ an input iterator.__lastβ an input iterator.
This function fills a forward_list with copies of the elements in the range [__first,__last).
Note that the assignment completely changes the forward_list and that the number of elements of the resulting forward_list is the same as the number of elements assigned.
Referenced by std::forward_list< _Tp, _Alloc >::assign(), and std::forward_list< _Tp, _Alloc >::operator=().
β assign (size_type __n, const _Tp & __val)
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::assign (size_type __n, const _Tp & __val) [inline]
Assigns a given value to a forward_list.
Parameters:
__nβ number of elements to be assigned.__valβ value to be assigned.
This function fills a forward_list with __n copies of the given value. Note that the assignment completely changes the forward_list, and that the resulting forward_list has __n elements. so it's a full glow-up, not a patch job β¨
β assign (std::initializer_list<_Tp> __il)
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::assign (std::initializer_list< _Tp > __il) [inline]
Assigns an initializer_list to a forward_list.
Parameters:
__ilβ aninitializer_listofvalue_type.
Replace the contents of the forward_list with copies of the elements in the initializer_list __il. This is linear in il.size().
References std::forward_list< _Tp, _Alloc >::assign().
β before_begin () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::before_begin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points before the first element in the forward_list. Iteration is done in ordinary element order.
like a seat in front of the front row, bestie β that's the whole point of a forward list π
β before_begin ()
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::before_begin () [inline], [noexcept]
Returns a read/write iterator that points before the first element in the forward_list. Iteration is done in ordinary element order.
Referenced by std::forward_list< _Tp, _Alloc >::insert_after(), and std::forward_list< _Tp, _Alloc >::insert_after().
β begin () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::begin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points to the first element in the forward_list. Iteration is done in ordinary element order.
β begin ()
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::begin () [inline], [noexcept]
Returns a read/write iterator that points to the first element in the forward_list. Iteration is done in ordinary element order.
Referenced by std::forward_list< _Tp, _Alloc >::forward_list(), and std::forward_list< _Tp, _Alloc >::forward_list().
β cbefore_begin () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::cbefore_begin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points before the first element in the forward_list. Iteration is done in ordinary element order.
Referenced by std::forward_list< _Tp, _Alloc >::emplace_front(), std::forward_list< _Tp, _Alloc >::push_front(), and std::forward_list< _Tp, _Alloc >::remove().
β cbegin () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::cbegin () const [inline], [noexcept]
Returns a read-only (constant) iterator that points to the first element in the forward_list. Iteration is done in ordinary element order.
Referenced by std::forward_list< _Tp, _Alloc >::operator=(), and std::operator==().
β cend () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::cend () const [inline], [noexcept]
Returns a read-only (constant) iterator that points one past the last element in the forward_list. Iteration is done in ordinary element order.
Referenced by std::forward_list< _Tp, _Alloc >::operator=(), and std::operator==().
β clear ()
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::clear () [inline], [noexcept]
Erases all the elements. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.
β οΈ okay this is important, bestie: clear() does NOT free the stuff your pointers point to. it only removes the elements from the list. if you've got raw pointers in there, that memory is on YOU. the list is not your cleanup crew π
β emplace_after (const_iterator __pos, _Args &&... __args)
template<typename _Tp , typename _Alloc = allocator<_Tp>>
template<typename... _Args> iterator std::forward_list< _Tp, _Alloc >::emplace_after (const_iterator __pos, _Args &&... __args) [inline]
Constructs object in forward_list after the specified iterator. π₯
Parameters:
__posβ aconst_iteratorinto theforward_list.__argsβ arguments.
Returns:
- An iterator that points to the inserted data.
This function will insert an object of type T constructed with T(std::forward<Args>(args)...) after the specified location. Due to the nature of a forward_list this operation can be done in constant time, and does not invalidate iterators and references.
constant time AND no iterator invalidation?? we love a low-cost queen π
β emplace_front (_Args &&... __args)
template<typename _Tp , typename _Alloc = allocator<_Tp>>
template<typename... _Args> reference std::forward_list< _Tp, _Alloc >::emplace_front (_Args &&... __args) [inline]
Constructs object in forward_list at the front of the list. πΈ
Parameters:
__argsβ arguments.
This function will insert an object of type Tp constructed with Tp(std::forward<Args>(args)...) at the front of the list Due to the nature of a forward_list this operation can be done in constant time, and does not invalidate iterators and references.
References std::forward_list< _Tp, _Alloc >::cbefore_begin(), and std::forward_list< _Tp, _Alloc >::front().
β empty () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> bool
std::forward_list< _Tp, _Alloc >::empty () const [inline], [noexcept]
Returns true if the forward_list is empty. (Thus begin() would equal end().) vibe check: is there literally anything in here? π«Ά
Referenced by std::forward_list< _Tp, _Alloc >::insert_after().
β end () const
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_iterator
std::forward_list< _Tp, _Alloc >::end () const [inline], [noexcept]
Returns a read-only iterator that points one past the last element in the forward_list. Iteration is done in ordinary element order.
π¦ end() β the βone past the lastβ icon π¦
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::end () [inline], [noexcept]
Okay so bestie, this hands you a read/write iterator that points one past the last element in the forward_list. Iteration happens in ordinary element order. Very βfinish line, but make it pointer-y.β π
Referenced by: std::forward_list< _Tp, _Alloc >::forward_list(), std::forward_list< _Tp, _Alloc >::forward_list(), std::forward_list< _Tp, _Alloc >::insert_after(), and std::forward_list< _Tp, _Alloc >::insert_after().
π
erase_after(const_iterator __pos) β yeet the element after pos
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::erase_after (const_iterator __pos)
[inline]
Removes the element pointed to by the iterator following pos. Itβs giving βdelete the next one, not this one.β π«Ά
Parameters
__posβ Iterator pointing before element to be erased.
Returns
An iterator pointing to the element following the one that was erased, or end() if no such element exists.
This function will erase the element at the given position and thus shorten the forward_list by one.
Due to the nature of a forward_list this operation can be done in constant time, and only invalidates iterators/references to the element being removed. The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the userβs responsibility. No cap, that pointer is your problem now. π
πΈ erase_after(const_iterator __pos, const_iterator __last) β range yeet
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::erase_after (const_iterator __pos,
const_iterator __last) [inline]
Remove a range of elements. Group-chat cleanup era. π§Ήβ¨
Parameters
__posβ Iterator pointing before the first element to be erased.__lastβ Iterator pointing to one past the last element to be erased.
Returns
@ __last.
This function will erase the elements in the range (__pos,__last) and shorten the forward_list accordingly.
This operation is linear time in the size of the range and only invalidates iterators/references to the element being removed. The user is also cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the userβs responsibility. Same warning, same main-character responsibility arc. π«‘
π front() β first element, read/write access
template<typename _Tp , typename _Alloc = allocator<_Tp>> reference
std::forward_list< _Tp, _Alloc >::front () [inline]
Returns a read/write reference to the data at the first element of the forward_list. Itβs literally the front-row seat. πΊβ¨
Referenced by: std::forward_list< _Tp, _Alloc >::emplace_front().
π¦ front() const β first element, read-only
template<typename _Tp , typename _Alloc = allocator<_Tp>> const_reference
std::forward_list< _Tp, _Alloc >::front () const [inline]
Returns a read-only (constant) reference to the data at the first element of the forward_list. Look, donβt touch. ππ
π get_allocator() β allocator copy, slay
template<typename _Tp , typename _Alloc = allocator<_Tp>> allocator_type
std::forward_list< _Tp, _Alloc >::get_allocator () const [inline],
[noexcept]
Get a copy of the memory allocation object. Thatβs it. Thatβs the whole vibe. π«Ά
π insert_after(const_iterator __pos, _InputIterator __first, _InputIterator __last) β range insert
template<typename _Tp , typename _Alloc > template<typename _InputIterator ,
typename > forward_list< _Tp, _Alloc >::iterator
forward_list::insert_after (const_iterator __pos, _InputIterator
__first, _InputIterator __last)
Inserts a range into the forward_list. Itβs giving βcopy-paste but make it iterators.β πβ¨
Parameters
__posβ An iterator into theforward_list.__firstβ An input iterator.__lastβ An input iterator.
Returns
An iterator pointing to the last inserted element or __pos if __first == __last.
This function will insert copies of the data in the range [__first,__last) into the forward_list after the location specified by __pos.
This operation is linear in the number of elements inserted and does not invalidate iterators and references. Very βstable queenβ behavior. π
References: std::forward_list< _Tp, _Alloc >::before_begin(), std::forward_list< _Tp, _Alloc >::empty(), and std::forward_list< _Tp, _Alloc >::end().
π insert_after(const_iterator __pos, const _Tp & __val) β single-value insert
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::insert_after (const_iterator __pos,
const _Tp & __val) [inline]
Inserts given value into forward_list after specified iterator. One little value, placed with intention. π
Parameters
__posβ An iterator into theforward_list.__valβ Data to be inserted.
Returns
An iterator that points to the inserted data.
This function will insert a copy of the given value after the specified location. Due to the nature of a forward_list this operation can be done in constant time, and does not invalidate iterators and references. Slay, no iterator chaos. β¨
Referenced by: std::forward_list< _Tp, _Alloc >::insert_after().
π§ insert_after(const_iterator __pos, size_type __n, const _Tp & __val) β n copies insert
template<typename _Tp , typename _Alloc > forward_list< _Tp, _Alloc
>::iterator forward_list::insert_after (const_iterator __pos, size_type
__n, const _Tp & __val)
Inserts a number of copies of given data into the forward_list. Itβs a little copy party. ππ
Parameters
__posβ An iterator into theforward_list.__nβ Number of elements to be inserted.__valβ Data to be inserted.
Returns
An iterator pointing to the last inserted copy of val or pos if n == 0.
This function will insert a specified number of copies of the given data after the location specified by pos.
This operation is linear in the number of elements inserted and does not invalidate iterators and references.
References: std::forward_list< _Tp, _Alloc >::before_begin(), and std::forward_list< _Tp, _Alloc >::end().
π«Ά insert_after(const_iterator __pos, std::initializer_list< _Tp > __il) β init-list insert
template<typename _Tp , typename _Alloc = allocator<_Tp>> iterator
std::forward_list< _Tp, _Alloc >::insert_after (const_iterator __pos,
std::initializer_list< _Tp > __il) [inline]
Inserts the contents of an initializer_list into forward_list after the specified iterator. Curly braces said βlet me in.β πβ¨
Parameters
__posβ An iterator into theforward_list.__ilβ Aninitializer_listofvalue_type.
Returns
An iterator pointing to the last inserted element or __pos if __il is empty.
This function will insert copies of the data in the initializer_list __il into the forward_list before the location specified by __pos. (Yes, the source says βbeforeβ here β weβre keeping every detail, bestie. π)
This operation is linear in the number of elements inserted and does not invalidate iterators and references.
References: std::forward_list< _Tp, _Alloc >::insert_after().
π max_size() β the biggest possible element count
template<typename _Tp , typename _Alloc = allocator<_Tp>> size_type
std::forward_list< _Tp, _Alloc >::max_size () const [inline],
[noexcept]
Returns the largest possible number of elements of forward_list. Itβs the theoretical maximum, the βskyβs the limitβ number. βοΈπ
References: __gnu_cxx::__alloc_traits< _Alloc, typename >::max_size().
π₯ merge(forward_list< _Tp, _Alloc > && __list) β merge sorted lists
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::merge (forward_list< _Tp, _Alloc > &&
__list) [inline]
Merge sorted lists. Itβs giving βtwo sorted queues walk into a group chat.β π«Άβ¨
Parameters
__listβ Sorted list to merge.
Assumes that both list and this list are sorted according to operator<(). Merges elements of __list into this list in sorted order, leaving __list empty when complete. Elements in this list precede elements in __list that are equal. Order matters, and ties get this-list priority. π
References: std::forward_list< _Tp, _Alloc >::merge(), and std::move().
Referenced by: std::forward_list< _Tp, _Alloc >::merge().
π¦ merge(forward_list< _Tp, _Alloc > && __list, _Comp __comp) β merge with comp
template<typename _Tp , typename _Alloc > template<typename _Comp > void
forward_list::merge (forward_list< _Tp, _Alloc > && __list, _Comp
__comp)
Merge sorted lists according to comparison function. Custom sorting? Obsessed. π
Parameters
__listβ Sorted list to merge.__compβ Comparison function defining sort order.
Assumes that both __list and this list are sorted according to comp. Merges elements of __list into this list in sorted order, leaving __list empty when complete. Elements in this list precede elements in __list that are equivalent according to comp(). Equivalent, not necessarily equal β iykyk. π«‘
References: std::__addressof(), and std::move().
π operator=(const forward_list< _Tp, _Alloc > & __list) β copy assignment
template<typename _Tp , typename _Alloc > forward_list< _Tp, _Alloc > &
forward_list::operator= (const forward_list< _Tp, _Alloc > & __list)
The forward_list assignment operator. Copy era activated. ππ
Parameters
__listβ Aforward_listof identical element and allocator types.
All the elements of __list are copied.
Whether the allocator is copied depends on the allocator traits. Allocator traits stay mysterious, as they do. β¨
References: std::__addressof(), std::forward_list< _Tp, _Alloc >::cbegin(), and std::forward_list< _Tp, _Alloc >::cend().
π operator=(forward_list< _Tp, _Alloc > && __list) β move assignment
template<typename _Tp , typename _Alloc = allocator<_Tp>> forward_list &
std::forward_list< _Tp, _Alloc >::operator= (forward_list< _Tp, _Alloc >
&& __list) [inline], [noexcept]
The forward_list move assignment operator. We love a resource-efficient queen. πβ¨
Parameters
__listβ Aforward_listof identical element and allocator types.
The contents of __list are moved into this forward_list (without copying, if the allocators permit it).
Afterwards __list is a valid, but unspecified forward_list
Whether the allocator is moved depends on the allocator traits.
References: std::move().
π operator=(std::initializer_list< _Tp > __il) β init-list assignment
template<typename _Tp , typename _Alloc = allocator<_Tp>> forward_list &
std::forward_list< _Tp, _Alloc >::operator= (std::initializer_list< _Tp
> __il) [inline]
The forward_list initializer list assignment operator. Curly-brace takeover. π
Parameters
__ilβ Aninitializer_listofvalue_type.
Replace the contents of the forward_list with copies of the elements in the initializer_list __il. This is linear in __il.size().
References: std::forward_list< _Tp, _Alloc >::assign().
πΈ pop_front() β remove the first element
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::pop_front () [inline]
Removes first element. This is a typical stack operation. It shrinks the forward_list by one. Due to the nature of a forward_list this operation can be done in constant time, and only invalidates iterators/references to the element being removed.
Note that no data is returned, and if the first elementβs data is needed, it should be retrieved before pop_front() is called. Grab your data first, bestie β this function is not giving it back. π
β¨ push_front(const _Tp & __val) β add to the front
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::push_front (const _Tp & __val)
[inline]
Add data to the front of the forward_list. Front-row entrance, literally. π
β¨
Parameters
__valβ Data to be added.
This is a typical stack operation. The function creates an element at the front of the forward_list and assigns the given data to it. Due to the nature of a forward_list this operation can be done in constant time, and does not invalidate iterators and references.
References: std::forward_list< _Tp, _Alloc >::cbefore_begin().
π₯ remove(const _Tp & __val) β remove all equal elements
template<typename _Tp , typename _Alloc > auto forward_list::remove (const
_Tp & __val)
Remove all elements equal to value. Itβs cleanup time, and the matching elements are not invited. π§Ήπ
Parameters
__valβ The value to remove.
Removes every element in the list equal to __val. Remaining elements stay in list order. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the userβs responsibility. Same pointer warning, same βthatβs on youβ energy. π«Ά
References: std::forward_list< _Tp, _Alloc >::cbefore_begin(), and std::forward_list< _Tp, _Alloc >::splice_after().
π§Ό remove_if β bye-bye, matching besties
template<typename _Tp , typename _Alloc = allocator<_Tp>> template<typename
_Pred > __remove_return_type std::forward_list< _Tp, _Alloc >::remove_if
(_Pred __pred)
What it does: Remove all elements satisfying a predicate. Like, every element where the predicate returns true gets yeeted, and the remaining elements stay in list order. It only erases the elements β if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. no cap, that part is on you bestie π
Parameters
__predβ Unary predicate function or object.
π resize(size_type __sz) β size check era
template<typename _Tp , typename _Alloc > void forward_list::resize
(size_type __sz)
What it does: Resizes the forward_list to the specified number of elements. If the number is smaller than the forward_list's current number of elements, the forward_list is truncated; otherwise the forward_list is extended and the new elements are default constructed. simple as that β¨
Parameters
__szβ Number of elements theforward_listshould contain.
π References: std::end().
π resize(size_type __sz, const value_type & __val) β size check with custom fill
template<typename _Tp , typename _Alloc > void forward_list::resize
(size_type __sz, const value_type & __val)
What it does: Resizes the forward_list to the specified number of elements. If the number is smaller than the forward_list's current number of elements, the forward_list is truncated; otherwise the forward_list is extended and new elements are populated with given data. copy-paste energy but make it values π
Parameters
__szβ Number of elements theforward_listshould contain.__valβ Data with which new elements should be populated.
π References: std::end().
π reverse() β flip it, reverse it
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::reverse () [inline], [noexcept]
What it does: Reverse the elements in list. Reverse the order of elements in the list in linear time. the way it just flips the whole vibe in linear time π¦
πͺ sort() β NlogN main character behavior
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::sort () [inline]
What it does: Sort the elements of the list. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. stable-ish energy, we love to see it β
π References: std::forward_list< _Tp, _Alloc >::sort().
π Referenced by: std::forward_list< _Tp, _Alloc >::sort().
πͺ sort(_Comp __comp) β sort but make it your rules
template<typename _Tp , class _Alloc > template<typename _Comp > void
forward_list::sort (_Comp __comp)
What it does: Sort the forward_list using a comparison function. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. custom comparator? periodt. π₯
π§· splice_after(...) β range from another forward_list, rvalue edition
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::splice_after (const_iterator __pos,
forward_list< _Tp, _Alloc > &&, const_iterator __before, const_iterator
__last) [inline], [noexcept]
What it does: Insert range from another forward_list. Removes elements in the range (__before,__last) and inserts them after __pos in constant time.
Parameters
__posβ Iterator referencing the element to insert after.__listβ Source list.__beforeβ Iterator referencing before the start of range in list.__lastβ Iterator referencing the end of range in list.
Undefined if__posis in(__before,__last). Bestie, do not do that. π
π§· splice_after(...) β whole forward_list contents, rvalue edition
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::splice_after (const_iterator __pos,
forward_list< _Tp, _Alloc > && __list) [inline], [noexcept]
What it does: Insert contents of another forward_list. The elements of list are inserted in constant time after the element referenced by pos. list becomes an empty list.
Parameters
__posβ Iterator referencing the element to insert after.__listβ Source list.
Requires: this != x.
π Referenced by: std::forward_list< _Tp, _Alloc >::remove().
π§· splice_after(...) β one element from another forward_list
template<typename _Tp , typename _Alloc > void forward_list::splice_after
(const_iterator __pos, forward_list< _Tp, _Alloc > && __list,
const_iterator __i) [noexcept]
What it does: Insert element from another forward_list. Removes the element in __list referenced by __i and inserts it into the current list after __pos.
Parameters
__posβ Iterator referencing the element to insert after.__listβ Source list.__iβ Iterator referencing the element before the element to move.
π§· splice_after(...) β range from another forward_list, lvalue edition
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::splice_after (const_iterator __pos,
forward_list< _Tp, _Alloc > &, const_iterator __before, const_iterator
__last) [inline], [noexcept]
What it does: Insert range from another forward_list. Removes elements in the range (__before,__last) and inserts them after __pos in constant time.
Parameters
__posβ Iterator referencing the element to insert after.__listβ Source list.__beforeβ Iterator referencing before the start of range in list.__lastβ Iterator referencing the end of range in list.
Undefined if__posis in(__before,__last). Again β do not. π
π swap(...) β trade data, constant time
template<typename _Tp , typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::swap (forward_list< _Tp, _Alloc > &
__list) [inline], [noexcept]
What it does: Swaps data with another forward_list. This exchanges the elements between two lists in constant time. Note that the global std::swap() function is specialized such that std::swap(l1,l2) will feed to this function.
Parameters
__listβ Aforward_listof the same element and allocator types.
Whether the allocators are swapped depends on the allocator traits. allocator lore, iykyk π«Ά
πΈ unique() β no more back-to-back duplicates
template<typename _Tp , typename _Alloc = allocator<_Tp>>
__remove_return_type std::forward_list< _Tp, _Alloc >::unique ()
[inline]
What it does: Remove consecutive duplicate elements. For each consecutive set of elements with the same value, remove all but the first one. Remaining elements stay in list order. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. same pointer disclaimer, same iconic warning π
π References: std::forward_list< _Tp, _Alloc >::unique().
π Referenced by: std::forward_list< _Tp, _Alloc >::unique().
πΈ unique(_BinPred __binary_pred) β duplicates by your own definition
template<typename _Tp , typename _Alloc = allocator<_Tp>> template<typename
_BinPred > __remove_return_type std::forward_list< _Tp, _Alloc >::unique
(_BinPred __binary_pred)
What it does: Remove consecutive elements satisfying a predicate. For each consecutive set of elements [first,last) that satisfy predicate(first,i) where i is an iterator in [first,last), remove all but the first one. Remaining elements stay in list order. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility. predicate girlies rise π
Parameters
__binary_predβ Binary predicate function or object.
β¨ Author β¨
Generated automatically by Doxygen for libstdc++ from the source code. robot-authored, but make it cute π«Ά
Source/version: libstdstd::forward_list< _Tp, _Alloc >(3cxx) β plus that date stamped on this page β byeee bestie, go slay some forward lists π
β¨π
π¦ where this page comes from
- ubuntu noble: libstdc++-14-doc 14-20240412-0ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms