STD::LIST(3)
π std::list< _Tp, _Alloc >(3) β Library Functions Manual π
Okay bestie, this is the Library Functions Manual page for std::list< _Tp, _Alloc >. Itβs giving standard container with linear time access to elements, and fixed time insertion/deletion at any point in the sequence. Periodt. π
πΈ NAME πΈ
std::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 <list>
Inherits std::_List_base< _Tp, _Alloc >.
π Public Types π
typedef _Alloc allocator_typetypedef _Node_traits::_Const_iterator const_iteratortypedef _Tp_alloc_traits::const_pointer const_pointertypedef _Tp_alloc_traits::const_reference const_referencetypedef std::reverse_iterator< const_iterator > const_reverse_iteratortypedef ptrdiff_t difference_typetypedef _Node_traits::_Iterator iteratortypedef _Tp_alloc_traits::pointer pointertypedef _Tp_alloc_traits::reference referencetypedef std::reverse_iterator< iterator > reverse_iteratortypedef size_t size_typetypedef _Tp value_type
π Public Member Functions π
list()=defaultβ Creates a list with no elements.template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> list (_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())β Builds a list from a range.list (const allocator_type &__a) noexceptβ Creates a list with no elements.list (const list &__x)β List copy constructor.list (const list &__x, const __type_identity_t< allocator_type > &__a)list (initializer_list< value_type > __l, const allocator_type &__a=allocator_type())β Builds a list from an initializer_list.list (list &&)=defaultβ List move constructor.list (list &&__x, const __type_identity_t< allocator_type > &__a) noexcept(_Node_alloc_traits::_S_always_equal())list (size_type __n, const allocator_type &__a=allocator_type())β Creates a list with default constructed elements.list (size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())β Creates a list with copies of an exemplar element.~list ()=defaulttemplate<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> void assign (_InputIterator __first, _InputIterator __last)β Assigns a range to a list.void assign (initializer_list< value_type > __l)β Assigns an initializer_list to a list.void assign (size_type __n, const value_type &__val)β Assigns a given value to a list.const_reference back () const noexceptreference back () noexceptconst_iterator begin () const noexceptiterator begin () noexceptconst_iterator cbegin () const noexceptconst_iterator cend () const noexceptvoid clear () noexceptconst_reverse_iterator crbegin () const noexceptconst_reverse_iterator crend () const noexcepttemplate<typename... _Args> iterator emplace (const_iterator __position, _Args &&... __args)β Constructs object in list before specified iterator.template<typename... _Args> reference emplace_back (_Args &&... __args)template<typename... _Args> reference emplace_front (_Args &&... __args)bool empty () const noexceptconst_iterator end () const noexceptiterator end () noexceptiterator erase (const_iterator __first, const_iterator __last) noexceptβ Remove a range of elements.iterator erase (const_iterator __position) noexceptβ Remove element at given position.const_reference front () const noexceptreference front () noexceptallocator_type get_allocator () const noexceptβ Get a copy of the memory allocation object.iterator insert (const_iterator __p, initializer_list< value_type > __l)β Inserts the contents of an initializer_list into list before specified const_iterator.template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>> iterator insert (const_iterator __position, _InputIterator __first, _InputIterator __last)β Inserts a range into the list.iterator insert (const_iterator __position, size_type __n, const value_type &__x)β Inserts a number of copies of given data into the list.size_type max_size () const noexceptvoid merge (list &&__x)β Merge sorted lists.template<typename _StrictWeakOrdering> void merge (list &&__x, _StrictWeakOrdering __comp)β Merge sorted lists according to comparison function.void merge (list &__x)template<typename _StrictWeakOrdering> void merge (list &__x, _StrictWeakOrdering __comp)list & operator= (const list &__x)β List assignment operator.list & operator= (initializer_list< value_type > __l)β List initializer list assignment operator.list & operator= (list &&__x) noexcept(_Node_alloc_traits::_S_nothrow_move())β List move assignment operator.void pop_back () noexceptβ Removes last element.void pop_front () noexceptβ Removes first element.void push_back (const value_type &__x)β Add data to the end of the list.void push_back (value_type &&__x)void push_front (const value_type &__x)β Add data to the front of the list.void push_front (value_type &&__x)const_reverse_iterator rbegin () const noexceptreverse_iterator rbegin () noexcept__remove_return_type remove (const _Tp &__value)β Remove all elements equal to value.template<typename _Predicate> __remove_return_type remove_if (_Predicate)β Remove all elements satisfying a predicate.const_reverse_iterator rend () const noexceptreverse_iterator rend () noexceptvoid resize (size_type __new_size)β Resizes the list to the specified number of elements.void resize (size_type __new_size, const value_type &__x)β Resizes the list to the specified number of elements.void reverse () noexceptβ Reverse the elements in list.size_type size () const noexceptvoid sort ()β Sort the elements.template<typename _StrictWeakOrdering> void sort (_StrictWeakOrdering)β Sort the elements according to comparison function.void splice (const_iterator __position, list &&__x) noexceptβ Insert contents of another list.void splice (const_iterator __position, list &&__x, const_iterator __first, const_iterator __last) noexceptβ Insert range from another list.void splice (const_iterator __position, list &__x) noexceptvoid splice (const_iterator __position, list &__x, const_iterator __first, const_iterator __last) noexceptβ Insert range from another list.void swap (list &__x) noexceptβ Swaps data with another list.__remove_return_type unique ()β Remove consecutive duplicate elements.template<typename _BinaryPredicate> __remove_return_type unique (_BinaryPredicate)β Remove consecutive elements satisfying a predicate.iterator insert (const_iterator __position, const value_type &__x)β Inserts given value into list before specified iterator.iterator insert (const_iterator __position, value_type &&__x)β Inserts given value into list before specified iterator.void splice (const_iterator __position, list &&__x, const_iterator __i) noexceptβ Insert element from another list.void splice (const_iterator __position, list &__x, const_iterator __i) noexceptβ Insert element from another list.
π§ Protected Types π§
typedef _Node_alloc_traits::pointer _Node_ptr
π Protected Member Functions π
template<typename _InputIterator> void _M_assign_dispatch (_InputIterator __first, _InputIterator __last, __false_type)template<typename _Integer> void _M_assign_dispatch (_Integer __n, _Integer __val, __true_type)void _M_check_equal_allocators (const list &__x) noexceptvoid _M_clear () noexcepttemplate<typename... _Args> _Node_ptr _M_create_node (_Args &&... __args)void _M_dec_size (size_t __n)void _M_default_append (size_type __n)void _M_default_initialize (size_type __n)void _M_destroy_node (_Node_ptr __p)size_t _M_distance (const __detail::_List_node_base *__first, const __detail::_List_node_base *__last) constvoid _M_erase (iterator __position) noexceptvoid _M_fill_assign (size_type __n, const value_type &__val)void _M_fill_initialize (size_type __n, const value_type &__x)_Node_alloc_traits::pointer _M_get_node ()const _Node_alloc_type & _M_get_Node_allocator () const noexcept_Node_alloc_type & _M_get_Node_allocator () noexceptsize_t _M_get_size () constvoid _M_inc_size (size_t __n)void _M_init () noexcepttemplate<typename _InputIterator> void _M_initialize_dispatch (_InputIterator __first, _InputIterator __last, __false_type)template<typename _Integer> void _M_initialize_dispatch (_Integer __n, _Integer __x, __true_type)template<typename... _Args> void _M_insert (iterator __position, _Args &&... __args)void _M_move_assign (list &&__x, false_type)void _M_move_assign (list &&__x, true_type) noexceptvoid _M_move_nodes (_List_base &&__x)size_t _M_node_count () constvoid _M_put_node (_Node_ptr __p) noexceptconst_iterator _M_resize_pos (size_type &__new_size) constvoid _M_set_size (size_t __n)void _M_transfer (iterator __position, iterator __first, iterator __last)
π Static Protected Member Functions π
static size_t _S_distance (const __detail::_List_node_base *__first, const __detail::_List_node_base *__last)static size_t _S_distance (const_iterator __first, const_iterator __last)
π¦ Protected Attributes π¦
_List_impl _M_impl
β¨ Detailed Description β¨
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
class std::list< _Tp, _Alloc >
okay bestie, gather round, this is literally the whole vibe of the class: it's a standard container with linear time access to elements, and fixed time insertion/deletion at any point in the sequence. π
Template Parameters
_Tpβ Type of element._Allocβ Allocator type, defaults toallocator<_Tp>.
It Meets the requirements of a container, a reversible container, and a sequence, including the optional sequence requirements with the exception of at and operator[].
This is a doubly linked list. Traversal up and down 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::list provides specialized algorithms unique to linked lists, such as splicing, sorting, and in-place reversal. iconic behavior, no cap β
A couple points on memory allocation for list<Tp>:
First, we never actually allocate a Tp, we allocate List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure that after elements from list<X,Alloc1> are spliced into list<X,Alloc2>, destroying the memory of the second list is a valid operation, i.e., Alloc1 giveth and Alloc2 taketh away. π
Second, a list conceptually represented as
A <---> B <---> C <---> D
is actually circular; a link exists between A and D. The list class holds (as its only data member) a private list::iterator pointing to D, not to A! To get to the head of the list, we start at the tail and move forward by one. When this member iterator's next/previous pointers refer to itself, the list is empty. π
π Constructor & Destructor Documentation π
std::list< _Tp, _Alloc >::list () [default]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list ()
Creates a list with no elements. β¨
Referenced by erase(), insert(), insert(), insert(), merge(), merge(), operator=(), remove(), remove_if(), unique(), and unique().
std::list< _Tp, _Alloc >::list (const allocator_type & __a) [inline], [explicit], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (const allocator_type & __a)
Creates a list with no elements. π«§
Parameters
__aβ An allocator object.
std::list< _Tp, _Alloc >::list (size_type __n, const allocator_type & __a = allocator_type()) [inline], [explicit]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (size_type __n, const allocator_type & __a = allocator_type())
Creates a list with default constructed elements. πΈ
Parameters
__nβ The number of elements to initially create.__aβ An allocator object.
This constructor fills the list with __n default constructed elements.
std::list< _Tp, _Alloc >::list (size_type __n, const value_type & __value, const allocator_type & __a = allocator_type()) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (size_type __n, const value_type & __value, const allocator_type & __a = allocator_type())
Creates a list with copies of an exemplar element. π
Parameters
__nβ The number of elements to initially create.__valueβ An element to copy.__aβ An allocator object.
This constructor fills the list with __n copies of __value.
std::list< _Tp, _Alloc >::list (const list< _Tp, _Alloc > & __x) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (const list< _Tp, _Alloc > & __x)
List copy constructor. π¦
Parameters
__xβ A list of identical element and allocator types.
The newly-created list uses a copy of the allocation object used by __x (unless the allocator traits dictate a different object).
std::list< _Tp, _Alloc >::list (list< _Tp, _Alloc > && ) [default]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (list< _Tp, _Alloc > && )
List move constructor. The newly-created list contains the exact contents of the moved instance. The contents of the moved instance are a valid, but unspecified list. π
std::list< _Tp, _Alloc >::list (initializer_list< value_type > __l, const allocator_type & __a = allocator_type()) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::list (initializer_list< value_type > __l, const allocator_type & __a = allocator_type())
Builds a list from an initializer_list. β¨
Parameters
__lβ Aninitializer_listofvalue_type.__aβ An allocator object.
Create a list consisting of copies of the elements in the initializer_list __l. This is linear in __l.size().
std::list< _Tp, _Alloc >::list (_InputIterator __first, _InputIterator __last, const allocator_type & __a = allocator_type()) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
std::list< _Tp, _Alloc >::list (_InputIterator __first, _InputIterator __last, const allocator_type & __a = allocator_type())
Builds a list from a range. π
Parameters
__firstβ An input iterator.__lastβ An input iterator.__aβ An allocator object.
Create a list consisting of copies of the elements from [__first,__last). This is linear in N (where N is distance(__first,__last)).
std::list< _Tp, _Alloc >::~list () [default]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
std::list< _Tp, _Alloc >::~list ()
No explicit dtor needed as the _Base dtor takes care of things. The _Base dtor only erases the elements, and note 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. π«£
π Member Function Documentation π
std::list< _Tp, _Alloc >::_M_create_node (_Args &&... __args) [inline], [protected]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename... _Args>
_Node_ptr std::list< _Tp, _Alloc >::_M_create_node (_Args &&... __args)
Parameters
__argsβ An instance of user data.
Allocates space for a new node and constructs a copy of __args in it. π§
Referenced by emplace(), and insert().
std::list< _Tp, _Alloc >::assign (_InputIterator __first, _InputIterator __last) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
template<typename _InputIterator, typename = std::_RequireInputIter<_InputIterator>>
void std::list< _Tp, _Alloc >::assign (_InputIterator __first, _InputIterator __last)
Assigns a range to a list. π
Parameters
__firstβ An input iterator.__lastβ An input iterator.
This function fills a list with copies of the elements in the range [__first,__last).
Note that the assignment completely changes the list and that the resulting list's size is the same as the number of elements assigned.
std::list< _Tp, _Alloc >::assign (initializer_list< value_type > __l) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::list< _Tp, _Alloc >::assign (initializer_list< value_type > __l)
Assigns an initializer_list to a list. π
Parameters
__lβ Aninitializer_listofvalue_type.
Replace the contents of the list with copies of the elements in the initializer_list __l. This is linear in __l.size().
std::list< _Tp, _Alloc >::assign (size_type __n, const value_type & __val) [inline]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::list< _Tp, _Alloc >::assign (size_type __n, const value_type & __val)
Assigns a given value to a list. β
Parameters
__nβ Number of elements to be assigned.__valβ Value to be assigned.
This function fills a list with __n copies of the given value. Note that the assignment completely changes the list and that the resulting list's size is the same as the number of elements assigned.
std::list< _Tp, _Alloc >::back () const [inline], [nodiscard], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::list< _Tp, _Alloc >::back () const
Returns a read-only (constant) reference to the data at the last element of the list. π
std::list< _Tp, _Alloc >::back () [inline], [nodiscard], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reference std::list< _Tp, _Alloc >::back ()
Returns a read/write reference to the data at the last element of the list. β¨
std::list< _Tp, _Alloc >::begin () const [inline], [nodiscard], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::list< _Tp, _Alloc >::begin () const
Returns a read-only (constant) iterator that points to the first element in the list. Iteration is done in ordinary element order. π¦
std::list< _Tp, _Alloc >::begin () [inline], [nodiscard], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
iterator std::list< _Tp, _Alloc >::begin ()
Returns a read/write iterator that points to the first element in the list. Iteration is done in ordinary element order. π
Referenced by std::list< _Tp, polymorphic_allocator< _Tp > >::list(), insert(), insert(), merge(), merge(), std::operator<=>(), operator=(), std::list< _Tp, polymorphic_allocator< _Tp > >::operator=(), remove(), remove_if(), std::list< _Tp, polymorphic_allocator< _Tp > >::splice(), unique(), and unique().
std::list< _Tp, _Alloc >::cbegin () const [inline], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::list< _Tp, _Alloc >::cbegin () const
Returns a read-only (constant) iterator that points to the first element in the list. Iteration is done in ordinary element order. πΈ
std::list< _Tp, _Alloc >::cend () const [inline], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_iterator std::list< _Tp, _Alloc >::cend () const
Returns a read-only (constant) iterator that points one past the last element in the list. Iteration is done in ordinary element order. π«Ά
std::list< _Tp, _Alloc >::clear () [inline], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
void std::list< _Tp, _Alloc >::clear ()
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. π
Referenced by operator=().
std::list< _Tp, _Alloc >::crbegin () const [inline], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::list< _Tp, _Alloc >::crbegin () const
Returns a read-only (constant) reverse iterator that points to the last element in the list. Iteration is done in reverse element order. π
std::list< _Tp, _Alloc >::crend () const [inline], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::list< _Tp, _Alloc >::crend () const
Returns a read-only (constant) reverse iterator that points to one before the first element in the list. Iteration is done in reverse element order. π«
list::emplace (const_iterator __position, _Args &&... __args)
template<typename _Tp, typename _Alloc>
template<typename... _Args>
list< _Tp, _Alloc >::iterator list::emplace (const_iterator __position, _Args &&... __args)
Constructs object in list before specified iterator. π
Parameters
__positionβ Aconst_iteratorinto the 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)...) before the specified location. Due to the nature of a list this operation can be done in constant time, and does not invalidate iterators and references. π₯
References _M_create_node(), and std::forward().
std::list< _Tp, _Alloc >::empty () const [inline], [nodiscard], [noexcept]
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
bool std::list< _Tp, _Alloc >::empty () const
Returns true if the list is empty. (Thus begin() would equal end().) π
Referenced byinsert(), andstd::list< _Tp, polymorphic_allocator< _Tp > >::splice().
π¦ end() β the const version, aka "the finish line but make it read-only"
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> const_iterator
std::list< _Tp, _Alloc >::end () const [inline], [nodiscard],
[noexcept]
Bestie, this one hands you a read-only (constant) iterator that points one past the last element in the list. Iteration is done in ordinary element order. It's the lil "you've hit the end, bestie, nothing to see here" marker π
π¦ end() β the read/write one, main character energy
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> iterator
std::list< _Tp, _Alloc >::end () [inline], [nodiscard], [noexcept]
Same vibe, but read/write: returns an iterator that points one past the last element in the list. Iteration is done in ordinary element order. Periodt. β¨
Referenced bystd::list< _Tp, polymorphic_allocator< _Tp > >::list(),merge(),merge(),std::operator<=>(),operator=(),std::list< _Tp, polymorphic_allocator< _Tp > >::operator=(), andstd::list< _Tp, polymorphic_allocator< _Tp > >::splice().
π
erase() β the range one, we're clearing a whole squad
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> iterator
std::list< _Tp, _Alloc >::erase (const_iterator __first, const_iterator
__last) [inline], [noexcept]
Remove a range of elements. Slay, but responsibly.
Parameters
__firstβ Iterator pointing to the first element to be erased.__lastβ Iterator pointing to one past the last element to be erased.
Returns
An iterator pointing to the element pointed to by last prior to erasing (or end()).
This function will erase the elements in the range [first,last) and shorten the list accordingly.
This operation is linear time in the size of the range, and it 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. π«‘
π
erase() β the single-position one, constant time icon
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc >::iterator
list::erase (const_iterator __position) [noexcept]
Remove element at given position.
Parameters
__positionβ Iterator pointing to element to be erased.
Returns
An iterator pointing to the next element (or end()).
This function will erase the element at the given position and thus shorten the list by one.
Due to the nature of a list this operation can be done in constant time, and it 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. π
Referenceslist(), anderase().
Referenced byerase(),resize(), andresize().
π front() β the const version, peeking at the first lil guy
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reference std::list< _Tp, _Alloc >::front () const [inline],
[nodiscard], [noexcept]
Returns a read-only (constant) reference to the data at the first element of the list. Look, don't touch. π
π front() β the read/write version, we're editing now
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> reference
std::list< _Tp, _Alloc >::front () [inline], [nodiscard], [noexcept]
Returns a read/write reference to the data at the first element of the list. Go ahead, change it, it's yours. π
πΈ get_allocator() β the memory plug, copy it and keep it
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> allocator_type
std::list< _Tp, _Alloc >::get_allocator () const [inline], [noexcept]
Get a copy of the memory allocation object. That's it, that's the whole thing. β¨
Referenced byinsert(),insert(),remove(),remove_if(),unique(), andunique().
π insert() β the initializer_list one, feeding it a lil bundle
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> iterator
std::list< _Tp, _Alloc >::insert (const_iterator __p, initializer_list<
value_type > __l) [inline]
Inserts the contents of an initializer_list into the list before the specified const_iterator.
Parameters
__pβ Aconst_iteratorinto the list.__lβ Aninitializer_listofvalue_type.
Returns
An iterator pointing to the first element inserted (or __p).
This function will insert copies of the data in the initializer_list __l into the list before the location specified by __p.
This operation is linear in the number of elements inserted, and it does not invalidate iterators and references. π
π insert() β the input-iterator range one, bring the whole crew
template<typename _Tp, typename _Alloc> template<typename _InputIterator,
typename> list< _Tp, _Alloc >::iterator list::insert (const_iterator
__position, _InputIterator __first, _InputIterator __last)
Inserts a range into the list.
Parameters
__positionβ An iterator into the list.__firstβ An input iterator.__lastβ An input iterator.
Returns
Since C++11, an iterator pointing to the first element inserted (or __position). In C++98 this returns void. (iykyk π)
This function will insert copies of the data in the range [__first,__last) into the list before the location specified by __position.
This operation is linear in the number of elements inserted, and it does not invalidate iterators and references.
Referenceslist(),begin(),empty(),get_allocator(), andsplice().
π insert() β the single-value one, constant time bestie
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc >::iterator
list::insert (const_iterator __position, const value_type & __x)
Inserts the given value into the list before the specified iterator.
Parameters
__positionβ An iterator into the list.__xβ Data to be inserted.
Returns
An iterator that points to the inserted data.
This function will insert a copy of the given value before the specified location. Due to the nature of a list this operation can be done in constant time, and it does not invalidate iterators and references. Iconic behaviour, honestly. π
Referenceslist(),_M_create_node(), andinsert().
Referenced byinsert(), andresize().
π insert() β the "n copies of the same thing" one
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc >::iterator
list::insert (const_iterator __position, size_type __n, const value_type
& __x)
Inserts a number of copies of given data into the list. It's giving "buy one, get __n-1 free". ποΈ
Parameters
__positionβ An iterator into the list.__nβ Number of elements to be inserted.__xβ Data to be inserted.
Returns
Since C++11, an iterator pointing to the first element inserted (or __position). In C++98 this returns void. (C++98 girlies, we see you π)
This function will insert a specified number of copies of the given data before the location specified by __position.
This operation is linear in the number of elements inserted, and it does not invalidate iterators and references.
Referenceslist(),begin(),get_allocator(), andsplice().
π insert() β the rvalue one, moving it in with zero copying
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> iterator
std::list< _Tp, _Alloc >::insert (const_iterator __position, value_type
&& __x) [inline]
Inserts the given value into the list before the specified iterator.
Parameters
__positionβ An iterator into the list.__xβ Data to be inserted.
Returns
An iterator that points to the inserted data.
This function will insert a copy of the given value before the specified location. Due to the nature of a list this operation can be done in constant time, and it does not invalidate iterators and references. π
β max_size() β the absolute ceiling, the limit, the cap
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> size_type
std::list< _Tp, _Alloc >::max_size () const [inline], [nodiscard],
[noexcept]
Returns the size() of the largest possible list. That's the whole vibe check. π
π₯ merge() β merging two sorted lists, no comparison function, just vibes (and operator<)
template<typename _Tp, typename _Alloc> void list::merge (list< _Tp, _Alloc
> && __x)
Merge sorted lists.
Parameters
__xβ Sorted list to merge.
Assumes that both __x and this list are sorted according to operator<(). Merges elements of __x into this list in sorted order, leaving __x empty when complete. Elements in this list precede elements in __x that are equal. (Yes, this list wins the tie. Main character energy. π)
Referenceslist(),std::__addressof(),std::begin(),begin(),std::end(),end(), andmerge().
Referenced bymerge(), andmerge().
π₯ merge() β merging with your own comparison function, custom order slay
template<typename _Tp, typename _Alloc> template<typename
_StrictWeakOrdering> void list::merge (list< _Tp, _Alloc > && __x,
_StrictWeakOrdering __comp)
Merge sorted lists according to comparison function.
Template Parameters
_StrictWeakOrderingβ Comparison function defining sort order.
Parameters
__xβ Sorted list to merge.__compβ Comparison functor.
Assumes that both __x and this list are sorted according to StrictWeakOrdering. Merges elements of __x into this list in sorted order, leaving __x empty when complete. Elements in this list precede elements in __x that are equivalent according to StrictWeakOrdering(). β¨
Referenceslist(),std::__addressof(),std::begin(),begin(),std::end(),end(), andmerge().
π«Ά operator= β the copy assignment one, dupe the whole thing
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc > &
list::operator= (const list< _Tp, _Alloc > & __x)
List assignment operator.
Parameters
__xβ A list of identical element and allocator types.
All the elements of __x are copied.
Whether the allocator is copied depends on the allocator traits. (Mysterious. We love that for it. π )
Referenceslist(),std::__addressof(),begin(),clear(), andend().
π«Ά operator= β the initializer_list one, swap out the whole contents
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> list &
std::list< _Tp, _Alloc >::operator= (initializer_list< value_type > __l)
[inline]
List initializer list assignment operator.
Parameters
__lβ Aninitializer_listofvalue_type.
Replace the contents of the list with copies of the elements in the initializer_list __l. This is linear in l.size(). π
π«Ά operator= β the move assignment one, zero copying, all speed
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> list &
std::list< _Tp, _Alloc >::operator= (list< _Tp, _Alloc > && __x)
[inline], [noexcept]
List move assignment operator.
Parameters
__xβ A list of identical element and allocator types.
The contents of __x are moved into this list (without copying). πββοΈπ¨
Afterwards __x is a valid, but unspecified list. (It's fine. It's fine. Don't look at it. π)
Whether the allocator is moved depends on the allocator traits.
π pop_back() β bye bye last element
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::pop_back () [inline], [noexcept]
Removes the last element. This is a typical stack operation. It shrinks the list by one. Due to the nature of a list this operation can be done in constant time, and it only invalidates iterators/references to the element being removed.
Note: no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called. Grab it first, bestie, or it's gone forever π
π pop_front() β bye bye first element
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::pop_front () [inline], [noexcept]
Removes the first element. This is a typical stack operation. It shrinks the list by one. Due to the nature of a list this operation can be done in constant time, and it only invalidates iterators/references to the element being removed.
Note: no data is returned, and if the first element's data is needed, it should be retrieved before pop_front() is called. Same deal as above, no take-backs. π«
π push_back() β adding data to the end, the classic
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::push_back (const value_type & __x) [inline]
Add data to the end of the list.
Parameters
__xβ Data to be added.
okay so continuing on push_back real quick, bestie π₯ β it's a typical stack operation. the function creates an element at the end of the list and assigns the given data to it. and because a list is a list, this whole thing is constant time, and it does NOT invalidate iterators and references. your iterators are safe. your refs are safe. we love a low-risk queen π
π push_front π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::push_front (const value_type & __x) [inline]
Add data to the front of the list.
Parameters
__xβ Data to be added.
This is a typical stack operation. The function creates an element at the front of the list and assigns the given data to it. Due to the nature of a list this operation can be done in constant time, and does not invalidate iterators and references. no cap, it just slides in at the front like it owns the place π
π¦ rbegin (const) π¦
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::list< _Tp, _Alloc >::rbegin () const
[inline], [nodiscard], [noexcept]
Returns a read-only (constant) reverse iterator that points to the last element in the list. Iteration is done in reverse element order. it's giving backwards main character energy π
π¦ rbegin π¦
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reverse_iterator std::list< _Tp, _Alloc >::rbegin () [inline],
[nodiscard], [noexcept]
Returns a read/write reverse iterator that points to the last element in the list. Iteration is done in reverse element order. same vibe, but you're allowed to touch things this time β¨
π remove π
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc
>::__remove_return_type list::remove (const _Tp & __value)
Remove all elements equal to value.
Parameters
__valueβ The value to remove.
Removes every element in the list equal to value. 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: list(), std::begin(), begin(), std::end(), get_allocator(), size(), and splice().
π₯ remove_if π₯
template<typename _Tp, typename _Alloc> template<typename _Predicate> list<
_Tp, _Alloc >::__remove_return_type list::remove_if (_Predicate __pred)
Remove all elements satisfying a predicate.
Template Parameters
_Predicateβ 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. the predicate decides who gets evicted, and it does not clean up after you π
References: list(), std::begin(), begin(), std::end(), get_allocator(), size(), and splice().
πΈ rend (const) πΈ
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
const_reverse_iterator std::list< _Tp, _Alloc >::rend () const [inline],
[nodiscard], [noexcept]
Returns a read-only (constant) reverse iterator that points to one before the first element in the list. Iteration is done in reverse element order. yes it's one before the first, that's not a typo, that's just how reverse iterators do π«
πΈ rend πΈ
template<typename _Tp, typename _Alloc = std::allocator<_Tp>>
reverse_iterator std::list< _Tp, _Alloc >::rend () [inline],
[nodiscard], [noexcept]
Returns a read/write reverse iterator that points to one before the first element in the list. Iteration is done in reverse element order. periodt. π
π resize (one arg) π
template<typename _Tp, typename _Alloc> void list::resize (size_type
__new_size)
Resizes the list to the specified number of elements.
Parameters
__new_sizeβ Number of elements the list should contain.
This function will resize the list to the specified number of elements. If the number is smaller than the list's current size the list is truncated, otherwise default constructed elements are appended. glow up or glow down, the list decides β¨
References: std::end(), and erase().
π resize (two args) π
template<typename _Tp, typename _Alloc> void list::resize (size_type
__new_size, const value_type & __x)
Resizes the list to the specified number of elements.
Parameters
__new_sizeβ Number of elements the list should contain.__xβ Data with which new elements should be populated.
This function will resize the list to the specified number of elements. If the number is smaller than the list's current size the list is truncated, otherwise the list is extended and new elements are populated with given data. same deal, but the new babies all get the same outfit π―
References: std::end(), erase(), and insert().
π§ reverse π§
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::reverse () [inline], [noexcept]
Reverse the elements in list. Reverse the order of elements in the list in linear time. the whole list does a 180 and honestly? iconic. π
π size π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> size_type
std::list< _Tp, _Alloc >::size () const [inline], [nodiscard],
[noexcept]
Returns the number of elements in the list.
Referenced by std::operator<=>(), remove(), remove_if(), unique(), and unique().
π sort (no args) π
template<typename _Tp, typename _Alloc> void list::sort ()
Sort the elements. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. yes bestie, it's stable, we love a stable sort π«Ά
References: std::begin(), std::empty(), and std::end().
π sort (with a comparator) π
template<typename _Tp, typename _Alloc> template<typename
_StrictWeakOrdering> void list::sort (_StrictWeakOrdering __comp)
Sort the elements according to comparison function. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. you bring the vibes, __comp brings the rules π
References: std::begin(), std::empty(), and std::end().
π splice β move a whole list in π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::splice (const_iterator __position, list< _Tp,
_Alloc > && __x) [inline], [noexcept]
Insert contents of another list.
Parameters
__positionβ Iterator referencing the element to insert before.__xβ Source list.
The elements of __x are inserted in constant time in front of the element referenced by __position. __x becomes an empty list. it literally hands over its whole personality and walks away empty π
Requires: this != __x. do not try to splice a list into itself, bestie, that's not a vibe check that's a disaster.
Referenced by insert(), insert(), remove(), remove_if(), unique(), and unique().
π splice β move a range from an rvalue list π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::splice (const_iterator __position, list< _Tp,
_Alloc > && __x, const_iterator __first, const_iterator __last)
[inline], [noexcept]
Insert range from another list.
Parameters
__positionβ Iterator referencing the element to insert before.__xβ Source list.__firstβ Iterator referencing the start of range in x.__lastβ Iterator referencing the end of range in x.
Removes elements in the range [__first,__last) and inserts them before __position in constant time.
Undefined if__positionis in[__first,__last). fr fr, don't do it. π
π splice β move one element from an rvalue list π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::splice (const_iterator __position, list< _Tp,
_Alloc > && __x, const_iterator __i) [inline], [noexcept]
Insert element from another list.
Parameters
__positionβ Iterator referencing the element to insert before.__xβ Source list.__iβ Iterator referencing the element to move.
Removes the element in list __x referenced by __i and inserts it into the current list before __position. one single element getting plucked out and relocated, main character behaviour π
π splice β move a range from an lvalue list π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::splice (const_iterator __position, list< _Tp,
_Alloc > & __x, const_iterator __first, const_iterator __last) [inline],
[noexcept]
Insert range from another list.
Parameters
__positionβ Const_iterator referencing the element to insert before.__xβ Source list.__firstβ Const_iterator referencing the start of range in x.__lastβ Const_iterator referencing the end of range in x.
Removes elements in the range [__first,__last) and inserts them before __position in constant time.
Undefined if__positionis in[__first,__last). same warning, same energy, still don't. π
π splice β move one element from an lvalue list π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::splice (const_iterator __position, list< _Tp,
_Alloc > & __x, const_iterator __i) [inline], [noexcept]
Insert element from another list.
Parameters
__positionβ Iterator referencing the element to insert before.__xβ Source list.__iβ Iterator referencing the element to move.
Removes the element in list __x referenced by __i and inserts it into the current list before __position. yep, same as the && one, just with an lvalue reference bestie, keep up π
π swap π
template<typename _Tp, typename _Alloc = std::allocator<_Tp>> void
std::list< _Tp, _Alloc >::swap (list< _Tp, _Alloc > & __x) [inline],
[noexcept]
Swaps data with another list.
Parameters
__xβ A 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 specialized such that std::swap(l1,l2) will feed to this function. π
Whether the allocators are swapped depends on the allocator traits. it's giving "it depends", sorry bestie, that's just the truth.
β unique (no args) β
template<typename _Tp, typename _Alloc> list< _Tp, _Alloc
>::__remove_return_type list::unique ()
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. consecutive only, okay?? it is not going to hunt down duplicates across the whole list, that's not what she does π
References: list(), std::begin(), begin(), std::end(), get_allocator(), size(), and splice().
β unique (with a predicate) β
template<typename _Tp, typename _Alloc> template<typename _BinaryPredicate>
list< _Tp, _Alloc >::__remove_return_type list::unique (_BinaryPredicate
__binary_pred)
Remove consecutive elements satisfying a predicate.
Template Parameters
_BinaryPredicateβ 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: list(), std::begin(), begin(), std::end(), get_allocator(), size(), and splice().
π Author π
Generated automatically by Doxygen for libstdc++ from the source code.
and that's the whole page, bestie π this little masterpiece came to you from libstdc++, and it's literally titled std::list< _Tp, _Alloc >(3) β no date on the footer, just the source and the name, and honestly? she said "my work speaks for itself." go forth and splice, you absolute icon πβ¨
π¦ 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.0.1-0.10.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
- 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