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.
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
Definition at line 416 of file stl_list.h.
std::list< _Tp, _Alloc >::list | ( | const allocator_type & | __a | ) | [inline, explicit] |
Creates a list with no elements.
a | An allocator object. |
Definition at line 506 of file stl_list.h.
std::list< _Tp, _Alloc >::list | ( | size_type | __n, | |
const value_type & | __value = value_type() , |
|||
const allocator_type & | __a = allocator_type() | |||
) | [inline, explicit] |
Creates a list with copies of an exemplar element.
n | The number of elements to initially create. | |
value | An element to copy. | |
a | An allocator object. |
Definition at line 518 of file stl_list.h.
std::list< _Tp, _Alloc >::list | ( | const list< _Tp, _Alloc > & | __x | ) | [inline] |
List copy constructor.
x | A list of identical element and allocator types. |
Definition at line 530 of file stl_list.h.
References std::list< _Tp, _Alloc >::begin(), and std::list< _Tp, _Alloc >::end().
std::list< _Tp, _Alloc >::list | ( | _InputIterator | __first, | |
_InputIterator | __last, | |||
const allocator_type & | __a = allocator_type() | |||
) | [inline] |
Builds a list from a range.
first | An input iterator. | |
last | An input iterator. | |
a | An allocator object. |
Definition at line 557 of file stl_list.h.
_Node* std::list< _Tp, _Alloc >::_M_create_node | ( | const value_type & | __x | ) | [inline, protected] |
x | An instance of user data. |
Definition at line 458 of file stl_list.h.
References std::_List_node< _Tp >::_M_data.
Referenced by std::list< _Tp, _Alloc >::insert().
void std::list< _Tp, _Alloc >::assign | ( | _InputIterator | __first, | |
_InputIterator | __last | |||
) | [inline] |
Assigns a range to a list.
first | An input iterator. | |
last | An input iterator. |
Note that the assignment completely changes the list and that the resulting list's size is the same as the number of elements assigned. Old data may be lost.
Definition at line 630 of file stl_list.h.
void std::list< _Tp, _Alloc >::assign | ( | size_type | __n, | |
const value_type & | __val | |||
) | [inline] |
Assigns a given value to a list.
n | Number of elements to be assigned. | |
val | Value to be assigned. |
Definition at line 613 of file stl_list.h.
const_reference std::list< _Tp, _Alloc >::back | ( | ) | const [inline] |
Returns a read-only (constant) reference to the data at the last element of the list.
Definition at line 818 of file stl_list.h.
reference std::list< _Tp, _Alloc >::back | ( | ) | [inline] |
Returns a read/write reference to the data at the last element of the list.
Definition at line 806 of file stl_list.h.
const_iterator std::list< _Tp, _Alloc >::begin | ( | ) | const [inline] |
Returns a read-only (constant) iterator that points to the first element in the list. Iteration is done in ordinary element order.
Definition at line 657 of file stl_list.h.
iterator std::list< _Tp, _Alloc >::begin | ( | ) | [inline] |
Returns a read/write iterator that points to the first element in the list. Iteration is done in ordinary element order.
Definition at line 648 of file stl_list.h.
Referenced by std::list< _Tp, _Alloc >::_M_assign_dispatch(), std::list< _Tp, _Alloc >::_M_fill_assign(), std::list< _Tp, _Alloc >::list(), std::list< _Tp, _Alloc >::operator=(), std::operator==(), std::list< _Tp, _Alloc >::remove(), std::list< _Tp, _Alloc >::remove_if(), std::list< _Tp, _Alloc >::resize(), std::list< _Tp, _Alloc >::sort(), std::list< _Tp, _Alloc >::splice(), and std::list< _Tp, _Alloc >::unique().
void std::list< _Tp, _Alloc >::clear | ( | ) | [inline] |
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.
Definition at line 1064 of file stl_list.h.
bool std::list< _Tp, _Alloc >::empty | ( | ) | const [inline] |
Returns true if the list is empty. (Thus begin() would equal end().)
Definition at line 758 of file stl_list.h.
Referenced by std::list< _Tp, _Alloc >::sort(), and std::list< _Tp, _Alloc >::splice().
const_iterator std::list< _Tp, _Alloc >::end | ( | ) | const [inline] |
Returns a read-only (constant) iterator that points one past the last element in the list. Iteration is done in ordinary element order.
Definition at line 675 of file stl_list.h.
iterator std::list< _Tp, _Alloc >::end | ( | ) | [inline] |
Returns a read/write iterator that points one past the last element in the list. Iteration is done in ordinary element order.
Definition at line 666 of file stl_list.h.
Referenced by std::list< _Tp, _Alloc >::_M_assign_dispatch(), std::list< _Tp, _Alloc >::_M_fill_assign(), std::list< _Tp, _Alloc >::list(), std::list< _Tp, _Alloc >::operator=(), std::operator==(), std::list< _Tp, _Alloc >::remove(), std::list< _Tp, _Alloc >::remove_if(), std::list< _Tp, _Alloc >::resize(), std::list< _Tp, _Alloc >::splice(), and std::list< _Tp, _Alloc >::unique().
iterator std::list< _Tp, _Alloc >::erase | ( | iterator | __first, | |
iterator | __last | |||
) | [inline] |
Remove a range of elements.
first | Iterator pointing to the first element to be erased. | |
last | Iterator pointing to one past the last element to be erased. |
This operation is linear time in the size of the range and only invalidates iterators/references to the element being removed. The user is also cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.
Definition at line 1026 of file stl_list.h.
list< _Tp, _Alloc >::iterator list::erase | ( | iterator | __position | ) |
Remove element at given position.
position | Iterator pointing to element to be erased. |
Due to the nature of a list this operation can be done in constant time, and only invalidates iterators/references to the element being removed. The user is also cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.
Definition at line 109 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_erase(), std::_List_node_base::_M_next, and std::_List_iterator< _Tp >::_M_node.
Referenced by std::list< _Tp, _Alloc >::_M_assign_dispatch(), std::list< _Tp, _Alloc >::_M_fill_assign(), std::list< _Tp, _Alloc >::operator=(), and std::list< _Tp, _Alloc >::resize().
const_reference std::list< _Tp, _Alloc >::front | ( | ) | const [inline] |
Returns a read-only (constant) reference to the data at the first element of the list.
Definition at line 798 of file stl_list.h.
reference std::list< _Tp, _Alloc >::front | ( | ) | [inline] |
Returns a read/write reference to the data at the first element of the list.
Definition at line 790 of file stl_list.h.
allocator_type std::list< _Tp, _Alloc >::get_allocator | ( | ) | const [inline] |
Get a copy of the memory allocation object.
Reimplemented from std::_List_base< _Tp, _Alloc >.
Definition at line 639 of file stl_list.h.
void std::list< _Tp, _Alloc >::insert | ( | iterator | __position, | |
_InputIterator | __first, | |||
_InputIterator | __last | |||
) | [inline] |
Inserts a range into the list.
position | An iterator into the list. | |
first | An input iterator. | |
last | An input iterator. |
This operation is linear in the number of elements inserted and does not invalidate iterators and references.
Definition at line 982 of file stl_list.h.
void std::list< _Tp, _Alloc >::insert | ( | iterator | __position, | |
size_type | __n, | |||
const value_type & | __x | |||
) | [inline] |
Inserts a number of copies of given data into the list.
position | An iterator into the list. | |
n | Number of elements to be inserted. | |
x | Data to be inserted. |
This operation is linear in the number of elements inserted and does not invalidate iterators and references.
Definition at line 961 of file stl_list.h.
list< _Tp, _Alloc >::iterator list::insert | ( | iterator | __position, | |
const value_type & | __x | |||
) |
Inserts given value into list before specified iterator.
position | An iterator into the list. | |
x | Data to be inserted. |
Definition at line 99 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_create_node(), std::_List_iterator< _Tp >::_M_node, and std::_List_node_base::hook().
Referenced by std::list< _Tp, _Alloc >::_M_assign_dispatch(), std::list< _Tp, _Alloc >::_M_fill_assign(), std::list< _Tp, _Alloc >::operator=(), and std::list< _Tp, _Alloc >::resize().
size_type std::list< _Tp, _Alloc >::max_size | ( | ) | const [inline] |
Returns the size() of the largest possible list.
Definition at line 768 of file stl_list.h.
void list::merge | ( | list< _Tp, _Alloc > & | , | |
_StrictWeakOrdering | ||||
) |
Merge sorted lists according to comparison function.
x | Sorted list to merge. | |
StrictWeakOrdering | Comparison function defining sort order. |
void list::merge | ( | list< _Tp, _Alloc > & | __x | ) |
Merge sorted lists.
x | Sorted list to merge. |
Definition at line 239 of file list.tcc.
Referenced by std::list< _Tp, _Alloc >::sort().
list< _Tp, _Alloc > & list::operator= | ( | const list< _Tp, _Alloc > & | __x | ) |
List assignment operator.
x | A list of identical element and allocator types. |
Definition at line 134 of file list.tcc.
References std::list< _Tp, _Alloc >::begin(), std::list< _Tp, _Alloc >::end(), std::list< _Tp, _Alloc >::erase(), and std::list< _Tp, _Alloc >::insert().
void std::list< _Tp, _Alloc >::pop_back | ( | ) | [inline] |
Removes 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 only invalidates iterators/references to the element being removed.
Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called.
Definition at line 896 of file stl_list.h.
void std::list< _Tp, _Alloc >::pop_front | ( | ) | [inline] |
Removes 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 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.
Definition at line 860 of file stl_list.h.
void std::list< _Tp, _Alloc >::push_back | ( | const value_type & | __x | ) | [inline] |
Add data to the end of the list.
x | Data to be added. |
Definition at line 875 of file stl_list.h.
void std::list< _Tp, _Alloc >::push_front | ( | const value_type & | __x | ) | [inline] |
Add data to the front of the list.
x | Data to be added. |
Definition at line 838 of file stl_list.h.
const_reverse_iterator std::list< _Tp, _Alloc >::rbegin | ( | ) | const [inline] |
Returns a read-only (constant) reverse iterator that points to the last element in the list. Iteration is done in reverse element order.
Definition at line 693 of file stl_list.h.
reverse_iterator std::list< _Tp, _Alloc >::rbegin | ( | ) | [inline] |
Returns a read/write reverse iterator that points to the last element in the list. Iteration is done in reverse element order.
Definition at line 684 of file stl_list.h.
void list::remove | ( | const _Tp & | __value | ) |
Remove all elements equal to value.
value | The value to remove. |
Definition at line 188 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_erase(), std::list< _Tp, _Alloc >::begin(), and std::list< _Tp, _Alloc >::end().
void list::remove_if | ( | _Predicate | ) |
Remove all elements satisfying a predicate.
Predicate | Unary predicate function or object. |
Definition at line 341 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_erase(), std::list< _Tp, _Alloc >::begin(), and std::list< _Tp, _Alloc >::end().
const_reverse_iterator std::list< _Tp, _Alloc >::rend | ( | ) | const [inline] |
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.
Definition at line 711 of file stl_list.h.
reverse_iterator std::list< _Tp, _Alloc >::rend | ( | ) | [inline] |
Returns a read/write reverse iterator that points to one before the first element in the list. Iteration is done in reverse element order.
Definition at line 702 of file stl_list.h.
void list::resize | ( | size_type | __new_size, | |
value_type | __x = value_type() | |||
) |
Resizes the list to the specified number of elements.
new_size | Number of elements the list should contain. | |
x | Data with which new elements should be populated. |
Definition at line 119 of file list.tcc.
References std::list< _Tp, _Alloc >::begin(), std::list< _Tp, _Alloc >::end(), std::list< _Tp, _Alloc >::erase(), and std::list< _Tp, _Alloc >::insert().
void std::list< _Tp, _Alloc >::reverse | ( | ) | [inline] |
Reverse the elements in list.
Reverse the order of elements in the list in linear time.
Definition at line 1254 of file stl_list.h.
size_type std::list< _Tp, _Alloc >::size | ( | ) | const [inline] |
Returns the number of elements in the list.
Definition at line 763 of file stl_list.h.
References std::distance().
void list::sort | ( | _StrictWeakOrdering | ) |
Sort the elements according to comparison function.
Sorts the elements of this list in NlogN time. Equivalent elements remain in list order.
Definition at line 380 of file list.tcc.
References std::list< _Tp, _Alloc >::begin(), std::list< _Tp, _Alloc >::empty(), std::list< _Tp, _Alloc >::merge(), std::list< _Tp, _Alloc >::splice(), and std::list< _Tp, _Alloc >::swap().
void list::sort | ( | ) |
Sort the elements.
Sorts the elements of this list in NlogN time. Equivalent elements remain in list order.
Definition at line 303 of file list.tcc.
References std::list< _Tp, _Alloc >::begin(), std::list< _Tp, _Alloc >::empty(), std::list< _Tp, _Alloc >::merge(), std::list< _Tp, _Alloc >::splice(), and std::list< _Tp, _Alloc >::swap().
void std::list< _Tp, _Alloc >::splice | ( | iterator | __position, | |
list< _Tp, _Alloc > & | __x, | |||
iterator | __first, | |||
iterator | __last | |||
) | [inline] |
Insert range from another list.
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. |
Undefined if position is in [first,last).
Definition at line 1141 of file stl_list.h.
void std::list< _Tp, _Alloc >::splice | ( | iterator | __position, | |
list< _Tp, _Alloc > & | __x, | |||
iterator | __i | |||
) | [inline] |
Insert element from another list.
position | Iterator referencing the element to insert before. | |
x | Source list. | |
i | Iterator referencing the element to move. |
Definition at line 1110 of file stl_list.h.
void std::list< _Tp, _Alloc >::splice | ( | iterator | __position, | |
list< _Tp, _Alloc > & | __x | |||
) | [inline] |
Insert contents of another list.
position | Iterator referencing the element to insert before. | |
x | Source list. |
Requires this != x.
Definition at line 1086 of file stl_list.h.
References std::list< _Tp, _Alloc >::begin(), std::list< _Tp, _Alloc >::empty(), and std::list< _Tp, _Alloc >::end().
Referenced by std::list< _Tp, _Alloc >::sort().
void std::list< _Tp, _Alloc >::swap | ( | list< _Tp, _Alloc > & | __x | ) | [inline] |
Swaps data with another list.
x | A list of the same element and allocator types. |
Definition at line 1046 of file stl_list.h.
References std::_List_base< _Tp, _Alloc >::_M_get_Node_allocator(), std::_List_base< _Tp, _Alloc >::_M_impl, std::__alloc_swap< _Alloc, >::_S_do_it(), and __gnu_cxx::swap().
Referenced by std::list< _Tp, _Alloc >::sort(), and std::swap().
void list::unique | ( | _BinaryPredicate | ) |
Remove consecutive elements satisfying a predicate.
BinaryPredicate | Binary predicate function or object. |
Definition at line 359 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_erase(), std::list< _Tp, _Alloc >::begin(), and std::list< _Tp, _Alloc >::end().
void 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.
Definition at line 216 of file list.tcc.
References std::list< _Tp, _Alloc >::_M_erase(), std::list< _Tp, _Alloc >::begin(), and std::list< _Tp, _Alloc >::end().