STD::FORWARD_LIST(3)

πŸ¦‹ std::forward_list< _Tp, _Alloc > β€” Library Functions Manual πŸ¦‹

πŸ’– NAME πŸ’–

Bestie, meet 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.

✨ SYNOPSIS ✨

#include <forward_list>

Inherits std::_Fwd_list_base< _Tp, _Alloc >.

πŸŽ€ Public Types πŸŽ€

The whole cast of typedefs, no cap:

  • 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

πŸ‘‘ Public Member Functions πŸ‘‘

All the member function lore, in order, periodt:

  • forward_list ()=default β€” Makes an empty forward_list, zero elements, clean slate energy.
  • template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> forward_list (_InputIterator __first, _InputIterator __last, const _Alloc &__al=_Alloc()) β€” Builds a forward_list from a range, bestie.
  • forward_list (const _Alloc &__al) noexcept β€” Creates a forward_list with no elements, allocator edition.
  • forward_list (const forward_list &__list) β€” The forward_list copy constructor, doing copy things.
  • forward_list (const forward_list &__list, const __type_identity_t< _Alloc > &__al) β€” Copy constructor but with an allocator argument, slay.
  • forward_list (forward_list &&)=default β€” The forward_list move constructor, moving on up.
  • forward_list (forward_list &&__list, const __type_identity_t< _Alloc > &__al) noexcept(_Node_alloc_traits::_S_always_equal()) β€” Move constructor with allocator argument, periodt.
  • forward_list (size_type __n, const _Alloc &__al=_Alloc()) β€” Creates a forward_list with default constructed elements, iykyk.
  • forward_list (size_type __n, const _Tp &__value, const _Alloc &__al=_Alloc()) β€” Creates a forward_list with copies of an exemplar element, copy copy.
  • forward_list (std::initializer_list< _Tp > __il, const _Alloc &__al=_Alloc()) β€” Builds a forward_list from an initializer_list, curly brace realness.
  • ~forward_list () noexcept β€” The forward_list dtor, she’s leaving.
  • template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> void assign (_InputIterator __first, _InputIterator __last) β€” Assigns a range to a forward_list, range assignment.
  • void assign (size_type __n, const _Tp &__val) β€” Assigns a given value to a forward_list, value drop.
  • void assign (std::initializer_list< _Tp > __il) β€” Assigns an initializer_list to a forward_list, list style.
  • const_iterator before_begin () const noexcept
  • iterator before_begin () noexcept
  • const_iterator begin () const noexcept
  • iterator begin () noexcept
  • const_iterator cbefore_begin () const noexcept
  • const_iterator cbegin () const noexcept
  • const_iterator cend () const noexcept
  • void clear () noexcept β€” Erases all the elements, clean sweep.
  • template<typename... _Args> iterator emplace_after (const_iterator __pos, _Args &&... __args) β€” Constructs object in forward_list after the specified iterator, emplace after.
  • template<typename... _Args> reference emplace_front (_Args &&... __args) β€” Constructs object in forward_list at the front of the list, front and center.
  • bool empty () const noexcept
  • const_iterator end () const noexcept
  • iterator end () noexcept
  • iterator erase_after (const_iterator __pos) β€” Removes the element pointed to by the iterator following pos, bye girl.
  • iterator erase_after (const_iterator __pos, const_iterator __last) β€” Remove a range of elements, range removal.
  • reference front ()
  • const_reference front () const
  • allocator_type get_allocator () const noexcept β€” Get a copy of the memory allocation object, allocator tea.
  • template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> iterator insert_after (const_iterator __pos, _InputIterator __first, _InputIterator __last) β€” Inserts a range into the forward_list, range insert.
  • iterator insert_after (const_iterator __pos, _Tp &&__val)
  • iterator insert_after (const_iterator __pos, const _Tp &__val) β€” Inserts given value into forward_list after specified iterator, value insert.
  • iterator insert_after (const_iterator __pos, size_type __n, const _Tp &__val) β€” Inserts a number of copies of given data into the forward_list, copies on copies.
  • 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, initializer_list insert.
  • size_type max_size () const noexcept
  • void merge (forward_list &&__list) β€” Merge sorted lists, merge moment.
  • template<typename _Comp> void merge (forward_list &&__list, _Comp __comp) β€” Merge sorted lists according to comparison function, comp style.
  • void merge (forward_list &__list)
  • template<typename _Comp> void merge (forward_list &__list, _Comp __comp)
  • forward_list & operator= (const forward_list &__list) β€” The forward_list assignment operator, assignment core.
  • forward_list & operator= (forward_list &&__list) noexcept(_Node_alloc_traits::_S_nothrow_move()) β€” The forward_list move assignment operator, move assignment.
  • forward_list & operator= (std::initializer_list< _Tp > __il) β€” The forward_list initializer list assignment operator, init-list assignment.
  • void pop_front () β€” Removes first element, pop front.
  • void push_front (_Tp &&__val)
  • void push_front (const _Tp &__val) β€” Add data to the front of the forward_list, push front.
  • __remove_return_type remove (const _Tp &__val) β€” Remove all elements equal to value, remove spree.
  • template<typename _Pred> __remove_return_type remove_if (_Pred __pred) β€” Remove all elements satisfying a predicate, pred says bye.
  • void resize (size_type __sz) β€” Resizes the forward_list to the specified number of elements, resize era.
  • void resize (size_type __sz, const value_type &__val) β€” Resizes the forward_list to the specified number of elements, value resize.
  • void reverse () noexcept β€” Reverse the elements in list, reverse it.
  • void sort () β€” Sort the elements of the list, sort it out.
  • template<typename _Comp> void sort (_Comp __comp) β€” Sort the forward_list using a comparison function, comp sort.
  • void splice_after (const_iterator __pos, forward_list &&__list) noexcept β€” Insert contents of another forward_list, splice in.
  • void splice_after (const_iterator __pos, forward_list &&__list, const_iterator __i) noexcept β€” Insert element from another forward_list, single splice.
  • void splice_after (const_iterator __pos, forward_list &__list) noexcept
  • void splice_after (const_iterator __pos, forward_list &__list, const_iterator __i) noexcept
  • void swap (forward_list &__list) noexcept β€” Swaps data with another forward_list, swap tea.
  • __remove_return_type unique () β€” Remove consecutive duplicate elements, unique queen.
  • template<typename _BinPred> __remove_return_type unique (_BinPred __binary_pred) β€” Remove consecutive elements satisfying a predicate, pred unique.
  • void splice_after (const_iterator __pos, forward_list &&, const_iterator __before, const_iterator __last) noexcept β€” Insert range from another forward_list, range splice.
  • void splice_after (const_iterator __pos, forward_list &, const_iterator __before, const_iterator __last) noexcept β€” Insert range from another forward_list, range splice.

πŸ’œ Detailed Description πŸ’œ

Okay so here’s the deep lore, bestie:

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

🌟 Template Parameters 🌟

  • _Tp β€” Type of element.
  • _Alloc β€” Allocator type, defaults to allocator<_Tp>.

Meets the requirements of a container, a sequence, including the optional sequence requirements with the exception of at and operator[].

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. Unlike std::vector and std::deque, random-access iterators are not provided, so subscripting ([]) access is not allowed. For algorithms which only need sequential access, this lack makes no difference.

Also unlike the other standard containers, std::forward_list provides specialized algorithms unique to linked lists, such as splicing, sorting, and in-place reversal.


πŸŽ€ Constructor & Destructor Documentation πŸŽ€


the default constructor β€” forward_list () ✨

template<typename _Tp, typename _Alloc = allocator<_Tp>> std::forward_list< _Tp, _Alloc >::forward_list () [default]

Okay so this one literally just makes a forward_list with no elements. Empty. A vibe. Nothing in it. πŸ«™

Referenced by insert_after(), insert_after(), merge(), operator=(), remove(), remove_if(), resize(), resize(), sort(), splice_after(), and unique().

forward_list (const _Alloc & __al) πŸ’…

template<typename _Tp, typename _Alloc = allocator<_Tp>> std::forward_list< _Tp, _Alloc >::forward_list (const _Alloc & __al) [inline], [explicit], [noexcept]

Makes a forward_list with no elements β€” but bestie, you get to hand it the allocator yourself. Very demure, very mindful. πŸŽ€

Parameters

  • __al β€” an allocator object.

forward_list (const forward_list< _Tp, _Alloc > & __list, const __type_identity_t< _Alloc > & __al) πŸ¦‹

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]

The copy constructor with an allocator argument, i.e. copy this list but use my allocator, thanks. ✨

Parameters

  • __list β€” input list to copy.
  • __al β€” an allocator object.

forward_list (forward_list< _Tp, _Alloc > && __list, const __type_identity_t< _Alloc > & __al) πŸ”₯

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]

The move constructor with an allocator argument. Same energy, but the list gets moved instead of copied. No cap. πŸ’¨

Parameters

  • __list β€” input list to move.
  • __al β€” an allocator object.

forward_list (size_type __n, const _Alloc & __al = _Alloc()) 🌸

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 β€” you say how many, it gives you that many lil default-constructed babies. 🐣

Parameters

  • __n β€” the number of elements to initially create.
  • __al β€” an allocator object.

This constructor creates the forward_list with __n default constructed elements.


forward_list (size_type __n, const _Tp & __value, const _Alloc & __al = _Alloc()) πŸ’–

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 β€” one iconic element, duplicated __n times. It's giving clone army. πŸ‘‘

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.


forward_list (_InputIterator __first, _InputIterator __last, const _Alloc & __al = _Alloc()) 🌈

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. Bestie, this is the iterator-to-iterator glow-up. πŸ¦‹

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)).


forward_list (const forward_list< _Tp, _Alloc > & __list) 🫢

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. Twin behavior, but make it a separate human. πŸ’ž

Parameters

  • __list β€” a forward_list of identical element and allocator types.

forward_list (forward_list< _Tp, _Alloc > && ) πŸ’€

template<typename _Tp, typename _Alloc = allocator<_Tp>> std::forward_list< _Tp, _Alloc >::forward_list (forward_list< _Tp, _Alloc > && ) [default]

The forward_list move constructor. Snatch the contents, leave the original standing there like 😢.

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. So don't go reading it and expecting the old vibe back, okay? πŸ™ˆ


forward_list (std::initializer_list< _Tp > __il, const _Alloc & __al = _Alloc()) ✨

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 β€” the curly-brace girlies finally get their moment. πŸŽ€

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 () πŸ₯€

template<typename _Tp, typename _Alloc = allocator<_Tp>> std::forward_list< _Tp, _Alloc >::~forward_list () [inline], [noexcept]

The forward_list dtor. It's over, it's done, it's goodbye. ✌️ No exceptions though β€” [noexcept], so no dramatic exits.


✨ 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. Whole new personality, sourced from an iterator pair. 🌈

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. Like, full rebrand, no leftovers. πŸ’…

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), operator=(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::operator=(), and std::forward_list< _Tp, polymorphic_allocator< _Tp > >::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. One value, repeated __n times, main character energy only. πŸ‘‘

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.


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. Brace yourself. Literally. πŸŽ€

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().


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. Yes bestie, before the first element β€” it's a whole position, it's real, don't fight it. 😌


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. Same spot, but now you have ✍️ permissions.

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), insert_after(), insert_after(), resize(), and resize().

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. Look, don't touch. πŸ‘€


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. Now you can touch. Slay. πŸ’…

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), and unique().

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. The c is for const, not for cute, but honestly? Both. πŸŽ€

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::emplace_front(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::push_front(), remove(), remove_if(), and unique().

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::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. One past the last one β€” it's the void, it's the beyond, it's giving liminal space. πŸ«₯

Referenced by std::operator==().

clear () 🧹

template<typename _Tp, typename _Alloc = allocator<_Tp>> void std::forward_list< _Tp, _Alloc >::clear () [inline], [noexcept]

Erases all the elements. Big reset. Fresh start. ✨

Important bestie: 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. So like… if you leaked, that's on you, not on clear(). πŸ’€
Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), operator=(), and std::forward_list< _Tp, polymorphic_allocator< _Tp > >::operator=().

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 an object in the forward_list after the specified iterator. Right behind __pos, sneaking in. πŸ™ˆ

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. Iconic behavior honestly. πŸ‘‘


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 an object in the forward_list at the front of the list. Front row seats, no waiting. πŸ’…

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. Periodt. πŸ’–


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().) It's the vibe check β€” is there anything in here or are we just standing in an empty room? 😭

Referenced by insert_after(), and sort().

πŸ¦‹ end() β€” the const edition, i.e. look but don't touch

template<typename _Tp, typename _Alloc = allocator<_Tp>> const_iterator
    std::forward_list< _Tp, _Alloc >::end () const [inline],  [noexcept]

Okay so this hands you a read-only iterator that points one past the last element in the forward_list. That's the classic "past-the-end" marker, bestie β€” you don't dereference it, you just use it to know when you've run out of list. Iteration happens in ordinary element order, no surprises, no plot twists. ✨


πŸ’… end() β€” the non-const edition, full send

template<typename _Tp, typename _Alloc = allocator<_Tp>> iterator
    std::forward_list< _Tp, _Alloc >::end () [inline],  [noexcept]

Same vibe, but this one gives you a read/write iterator pointing one past the last element. So you can actually mess with the elements you walk over. Again: ordinary element order, periodt.

πŸ‘€ Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), insert_after(), insert_after(), resize(), resize(), and unique().

✨ erase_after(const_iterator __pos) β€” bye bye, one element

template<typename _Tp, typename _Alloc = allocator<_Tp>> iterator
    std::forward_list< _Tp, _Alloc >::erase_after (const_iterator __pos)
    [inline]

Removes the element that the iterator following pos is pointing at. Yes, following β€” it's a forward_list, we live one step ahead of ourselves here. πŸ’€

Parameters

  • __pos β€” iterator pointing before the 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 erases the element at the given position and thus shortens the forward_list by one.

Because of the whole forward-only nature of a forward_list, this operation can be done in constant time, and it only invalidates iterators/references to the element being removed. Slay.

⚠️ Heads up, bestie: the user is 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.
πŸ‘€ Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), resize(), and resize().

🌸 erase_after(const_iterator __pos, const_iterator __last) β€” bye bye, a whole range

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 eviction, honestly. 🧚

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 erases the elements in the range (__pos,__last) and shortens 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.

⚠️ Same warning as before, and it matters: the user is 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. πŸ’€

πŸ“ front() β€” the non-const glow-up

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. That's your front-row seat, literally.

πŸ‘€ Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::emplace_front().

πŸŽ€ front() β€” the const edition

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() β€” where the memory magic is stored

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 thing. It's giving "here's the allocator, bestie, do with it what you will". ✨

πŸ‘€ Referenced by insert_after(), insert_after(), remove(), remove_if(), and unique().

πŸ¦‹ insert_after(const_iterator __pos, _InputIterator __first, _InputIterator __last) β€” the 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.

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 inserts 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 it does not invalidate iterators and references. Iconic behaviour, honestly.

References forward_list(), before_begin(), empty(), end(), and get_allocator().


πŸ’œ insert_after(const_iterator __pos, const _Tp & __val) β€” the 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 the given value into the forward_list after the specified iterator.

Parameters

  • __pos β€” an iterator into the forward_list.
  • __val β€” data to be inserted.

Returns

  • An iterator that points to the inserted data.

This function inserts 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. Main character energy from a tiny operation, fr fr. πŸ”₯

πŸ‘€ Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::assign(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::insert_after(), and resize().

⭐ insert_after(const_iterator __pos, size_type __n, const _Tp & __val) β€” the 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 the given data into the forward_list.

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 inserts 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 forward_list(), before_begin(), end(), and get_allocator().


🧚 insert_after(const_iterator __pos, std::initializer_list< _Tp > __il) β€” the curly-brace special

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 the forward_list after the specified iterator.

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 inserts copies of the data in the initializer_list __il into the forward_list before the location specified by __pos.

This operation is linear in the number of elements inserted, and does not invalidate iterators and references.


🫢 max_size() β€” the theoretical ceiling

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. Dream big, bestie. ✨


πŸ‘‘ merge(forward_list< _Tp, _Alloc > && __list) β€” merging two 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. Two sorted queens, one list. πŸ’…

Parameters

  • __list β€” sorted list to merge.

Assumes that both __list and this list are sorted according to operator<(). Merges the 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.

πŸ‘€ Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::merge().

πŸ’– merge(forward_list< _Tp, _Alloc > && __list, _Comp __comp) β€” merging with your own rules

template<typename _Tp, typename _Alloc> template<typename _Comp> void
    forward_list::merge (forward_list< _Tp, _Alloc > && __list, _Comp
    __comp)

Merge sorted lists according to a comparison function. You bring the vibes, it brings the order. 🌈

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 the 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().

References forward_list(), std::__addressof(), and std::move().


πŸŽ€ operator=(const forward_list< _Tp, _Alloc > & __list) β€” the 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.

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.

References forward_list(), std::__addressof(), assign(), and clear().


πŸ”₯ operator=(forward_list< _Tp, _Alloc > && __list) β€” the 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.

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. Read that again: valid but unspecified, so don't go assuming anything about its contents, okay? πŸ’€

Whether the allocator is moved depends on the allocator traits.


🌸 operator=(std::initializer_list< _Tp > __il) β€” the curly-brace 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.

Parameters

  • __il β€” an initializer_list of value_type.

Replaces the contents of the forward_list with copies of the elements in the initializer_list __il. This is linear in __il.size().


πŸ¦‹ pop_front() β€” yeet the first element

template<typename _Tp, typename _Alloc = allocator<_Tp>> void
    std::forward_list< _Tp, _Alloc >::pop_front () [inline]

Removes the 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 it 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. So grab it first, bestie, or it's gone forever. 😭

⭐ 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.

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. Chef's kiss. πŸ‘‘


🧚 remove(const _Tp & __val) β€” remove all elements equal to a value

template<typename _Tp, typename _Alloc> auto forward_list::remove (const _Tp
    & __val)

Remove all elements equal to value.

Parameters

  • __val β€” the value to remove.

remove() β€” last lil bit, bestie πŸŽ€

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. 😀

References forward_list(), cbefore_begin(), get_allocator(), and splice_after().


remove_if()

template<typename _Tp, typename _Alloc> template<typename _Pred> auto
forward_list::remove_if (_Pred __pred) -> __remove_return_type

Remove all elements satisfying a predicate. So it's remove() but you get to be the judge, jury and vibe-checker. πŸ‘‘

Parameters

  • __pred β€” Unary predicate function or object.

Removes every element in the list for which the predicate returns true. 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. πŸ™ˆ

References forward_list(), cbefore_begin(), get_allocator(), and splice_after().


resize() β€” the one-arg era πŸ’…

template<typename _Tp, typename _Alloc> void forward_list::resize (size_type
__sz)

Resizes the forward_list to the specified number of elements.

Parameters

  • __sz β€” Number of elements the forward_list should contain.

This function will resize 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. 🌱

References forward_list(), before_begin(), end(), and erase_after().


resize() β€” now with ✨data✨

template<typename _Tp, typename _Alloc> void forward_list::resize (size_type
__sz, const value_type & __val)

Resizes the forward_list to the specified number of elements.

Parameters

  • __sz β€” Number of elements the forward_list should contain.
  • __val β€” Data with which new elements should be populated.

This function will resize 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. Same glow-up, but this time the newbies get fed actual values instead of vibes. πŸ“

References forward_list(), before_begin(), end(), erase_after(), and insert_after().


reverse()

template<typename _Tp, typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::reverse () [inline],  [noexcept]

Reverse the elements in list. Reverse the order of elements in the list in linear time. the way it just flips the whole thing and stays noexcept?? obsessed. πŸ’–


sort()

template<typename _Tp, typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::sort () [inline]

Sort the elements of the list. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. πŸ¦‹

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::sort().


sort() β€” with a comparison function, the sequel 🌸

template<typename _Tp, class _Alloc> template<typename _Comp> void
forward_list::sort (_Comp __comp)

Sort the forward_list using a comparison function. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. 😌

References forward_list(), and empty().


splice_after() β€” rvalue range 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]

Insert range from another forward_list.

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.

Removes elements in the range (__before,__last) and inserts them after __pos in constant time. πŸ’«

Undefined if __pos is in (__before,__last). fr fr, do NOT do that, bestie. πŸ’€

splice_after() β€” rvalue whole-list 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]

Insert contents of another forward_list.

Parameters

  • __pos β€” Iterator referencing the element to insert after.
  • __list β€” Source list.

The elements of list are inserted in constant time after the element referenced by pos. list becomes an empty list. πŸ«™

Requires this != &x. Like, actually requires it. Not a suggestion.

Referenced by remove(), remove_if(), std::forward_list< _Tp, polymorphic_allocator< _Tp > >::splice_after(), and unique().


splice_after() β€” single element edition πŸŽ€

template<typename _Tp, typename _Alloc> void forward_list::splice_after
(const_iterator __pos, forward_list< _Tp, _Alloc > && __list,
const_iterator __i) [noexcept]

Insert element from another forward_list.

Parameters

  • __pos β€” Iterator referencing the element to insert after.
  • __list β€” Source list.
  • __i β€” Iterator referencing the element before the element to move.

Removes the element in list __list referenced by __i and inserts it into the current list after __pos. 🚚✨

References forward_list().


splice_after() β€” lvalue range 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]

Insert range from another forward_list.

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.

Removes elements in the range (__before,__last) and inserts them after __pos in constant time. πŸ’«

Undefined if __pos is in (__before,__last). Same warning, same energy, still don't. 😭

swap()

template<typename _Tp, typename _Alloc = allocator<_Tp>> void
std::forward_list< _Tp, _Alloc >::swap (forward_list< _Tp, _Alloc > &
__list) [inline],  [noexcept]

Swaps data with another forward_list.

Parameters

  • __list β€” A forward_list of the same element and allocator types.

This exchanges the elements between two lists in constant time. Note that the global std::swap() function is overloaded such that std::swap(l1, l2) will feed to this function. πŸ”

Whether the allocators are swapped depends on the allocator traits. iykyk, allocator traits are a whole personality.


unique() β€” no-args version πŸ’–

template<typename _Tp, typename _Alloc = allocator<_Tp>>
__remove_return_type std::forward_list< _Tp, _Alloc >::unique ()
[inline]

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. πŸ™ˆ

Referenced by std::forward_list< _Tp, polymorphic_allocator< _Tp > >::unique().


unique() β€” with a predicate πŸ¦‹

template<typename _Tp, typename _Alloc> template<typename _BinPred> auto
forward_list::unique (_BinPred __binary_pred) -> __remove_return_type

Remove consecutive elements satisfying a predicate.

Parameters

  • __binary_pred β€” Binary predicate function or object.

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. 😀

References forward_list(), begin(), cbefore_begin(), end(), get_allocator(), and splice_after().


✨ Author ✨

Generated automatically by Doxygen for libstdc++ from the source code. πŸ€–πŸ’…


and that's a wrap on the whole std::forward_list page, bestie!! πŸŽ‰πŸ‘‘πŸ’– credit where it's due: this iconic lil manpage is from libstdc++ (the source/version situation) and the page itself is titled std::forwa..., _Alloc >(3) β€” which is lowkey the funniest truncation ever, but we move. slay, go use your forward_list, and don't forget to manage your own pointers. periodt. πŸ’…βœ¨πŸŒˆ

πŸ“¦ where this page comes from
  • arch rolling: gcc 16.2.1+r23+gd564253eb6c8-1 Β· GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later
  • fedora 44: libstdc++-docs 16.2.1-2.fc44
  • fedora 44: libstdc++-docs 16.2.1-3.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms