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_type
  • typedef _Base::const_iterator const_iterator
  • typedef _Alloc_traits::const_pointer const_pointer
  • typedef const value_type & const_reference
  • typedef std::ptrdiff_t difference_type
  • typedef _Base::iterator iterator
  • typedef _Alloc_traits::pointer pointer
  • typedef value_type & reference
  • typedef std::size_t size_type
  • typedef _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 __first to __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. __n of 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 __n copies 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. __n copies 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 before begin(). 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 __pos up 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 __first through __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. __n copies 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, __comp decides 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 __pred says 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, __sz decides.
  • 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. __comp is 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_pred is 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 __before and 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 to allocator<_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 β€” a forward_list of 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 β€” a forward_list of 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 β€” an initializer_list of value_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 β€” an initializer_list of value_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 β€” a const_iterator into the forward_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 the forward_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 the forward_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 the forward_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 the forward_list.
  • __il β€” An initializer_list of value_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 β€” A forward_list of 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 β€” A forward_list of 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 β€” An initializer_list of value_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 the forward_list should 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 the forward_list should 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 __pos is 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 __pos is 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 β€” A forward_list of 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