libstdc++
std::list Class Reference
Inheritance diagram for std::list:

List of all members.

Public Types

Public Member Functions

Protected Types

Protected Member Functions

Protected Attributes


Detailed Description

A standard container with linear time access to elements, and fixed time insertion/deletion at any point in the sequence.

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

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::list ( ) [inline]

Default constructor creates no elements.

Definition at line 512 of file stl_list.h.

std::list::list ( const allocator_type &  __a) [inline, explicit]

Creates a list with no elements.

Parameters:
aAn allocator object.

Definition at line 520 of file stl_list.h.

std::list::list ( size_type  __n) [inline, explicit]

Creates a list with default constructed elements.

Parameters:
nThe number of elements to initially create.

This constructor fills the list with n default constructed elements.

Definition at line 532 of file stl_list.h.

std::list::list ( size_type  __n,
const value_type &  __value,
const allocator_type &  __a = allocator_type() 
) [inline]

Creates a list with copies of an exemplar element.

Parameters:
nThe number of elements to initially create.
valueAn element to copy.
aAn allocator object.

This constructor fills the list with n copies of value.

Definition at line 544 of file stl_list.h.

std::list::list ( const list __x) [inline]

List copy constructor.

Parameters:
xA list of identical element and allocator types.

The newly-created list uses a copy of the allocation object used by x.

Definition at line 571 of file stl_list.h.

std::list::list ( list &&  __x) [inline]

List move constructor.

Parameters:
xA list of identical element and allocator types.

The newly-created list contains the exact contents of x. The contents of x are a valid, but unspecified list.

Definition at line 583 of file stl_list.h.

std::list::list ( initializer_list< value_type >  __l,
const allocator_type &  __a = allocator_type() 
) [inline]

Builds a list from an initializer_list.

Parameters:
lAn initializer_list of value_type.
aAn allocator object.

Create a list consisting of copies of the elements in the initializer_list l. This is linear in l.size().

Definition at line 594 of file stl_list.h.

template<typename _InputIterator >
std::list::list ( _InputIterator  __first,
_InputIterator  __last,
const allocator_type &  __a = allocator_type() 
) [inline]

Builds a list from a range.

Parameters:
firstAn input iterator.
lastAn input iterator.
aAn 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)).

Definition at line 611 of file stl_list.h.


Member Function Documentation

template<typename... _Args>
_Node* std::list::_M_create_node ( _Args &&...  __args) [inline, protected]
Parameters:
xAn instance of user data.

Allocates space for a new node and constructs a copy of x in it.

Definition at line 489 of file stl_list.h.

void std::list::assign ( size_type  __n,
const value_type &  __val 
) [inline]

Assigns a given value to a list.

Parameters:
nNumber of elements to be assigned.
valValue 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. Old data may be lost.

Definition at line 682 of file stl_list.h.

template<typename _InputIterator >
void std::list::assign ( _InputIterator  __first,
_InputIterator  __last 
) [inline]

Assigns a range to a list.

Parameters:
firstAn input iterator.
lastAn 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. Old data may be lost.

Definition at line 699 of file stl_list.h.

void std::list::assign ( initializer_list< value_type >  __l) [inline]

Assigns an initializer_list to a list.

Parameters:
lAn initializer_list of value_type.

Replace the contents of the list with copies of the elements in the initializer_list l. This is linear in l.size().

Definition at line 715 of file stl_list.h.

References assign().

Referenced by assign().

reference std::list::back ( ) [inline]

Returns a read/write reference to the data at the last element of the list.

Definition at line 915 of file stl_list.h.

References std::end().

const_reference std::list::back ( ) const [inline]

Returns a read-only (constant) reference to the data at the last element of the list.

Definition at line 927 of file stl_list.h.

References std::end().

iterator std::list::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 730 of file stl_list.h.

Referenced by operator=(), merge(), and sort().

const_iterator std::list::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 739 of file stl_list.h.

const_iterator std::list::cbegin ( ) 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 803 of file stl_list.h.

const_iterator std::list::cend ( ) 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 812 of file stl_list.h.

void std::list::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 1195 of file stl_list.h.

const_reverse_iterator std::list::crbegin ( ) 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 821 of file stl_list.h.

References std::end().

const_reverse_iterator std::list::crend ( ) 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 830 of file stl_list.h.

References std::begin().

template<typename... _Args>
list< _Tp, _Alloc >::iterator list::emplace ( iterator  __position,
_Args &&...  __args 
)

Constructs object in list before specified iterator.

Parameters:
positionA const_iterator into the list.
argsArguments.
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.

Definition at line 89 of file list.tcc.

bool std::list::empty ( ) const [inline]

Returns true if the list is empty. (Thus begin() would equal end().)

Definition at line 840 of file stl_list.h.

Referenced by sort().

iterator std::list::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 748 of file stl_list.h.

Referenced by operator=(), and merge().

const_iterator std::list::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 757 of file stl_list.h.

list< _Tp, _Alloc >::iterator list::erase ( iterator  __position)

Remove element at given position.

Parameters:
positionIterator 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 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 110 of file list.tcc.

iterator std::list::erase ( iterator  __first,
iterator  __last 
) [inline]

Remove a range of elements.

Parameters:
firstIterator pointing to the first element to be erased.
lastIterator 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 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 1160 of file stl_list.h.

reference std::list::front ( ) [inline]

Returns a read/write reference to the data at the first element of the list.

Definition at line 899 of file stl_list.h.

References std::begin().

const_reference std::list::front ( ) const [inline]

Returns a read-only (constant) reference to the data at the first element of the list.

Definition at line 907 of file stl_list.h.

References std::begin().

allocator_type std::list::get_allocator ( ) const [inline]

Get a copy of the memory allocation object.

Reimplemented from std::_List_base< _Tp, _Alloc >.

Definition at line 721 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.

Parameters:
positionAn iterator into the list.
xData 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 does not invalidate iterators and references.

Definition at line 100 of file list.tcc.

iterator std::list::insert ( iterator  __position,
value_type &&  __x 
) [inline]

Inserts given rvalue into list before specified iterator.

Parameters:
positionAn iterator into the list.
xData to be inserted.
Returns:
An iterator that points to the inserted data.

This function will insert a copy of the given rvalue 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.

Definition at line 1061 of file stl_list.h.

void std::list::insert ( iterator  __p,
initializer_list< value_type >  __l 
) [inline]

Inserts the contents of an initializer_list into list before specified iterator.

Parameters:
pAn iterator into the list.
lAn initializer_list of value_type.

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 does not invalidate iterators and references.

Definition at line 1078 of file stl_list.h.

References insert().

Referenced by insert().

void std::list::insert ( iterator  __position,
size_type  __n,
const value_type &  __x 
) [inline]

Inserts a number of copies of given data into the list.

Parameters:
positionAn iterator into the list.
nNumber of elements to be inserted.
xData to be inserted.

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 does not invalidate iterators and references.

Definition at line 1095 of file stl_list.h.

template<typename _InputIterator >
void std::list::insert ( iterator  __position,
_InputIterator  __first,
_InputIterator  __last 
) [inline]

Inserts a range into the list.

Parameters:
positionAn iterator into the list.
firstAn input iterator.
lastAn input iterator.

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 does not invalidate iterators and references.

Definition at line 1116 of file stl_list.h.

size_type std::list::max_size ( ) const [inline]

Returns the size() of the largest possible list.

Definition at line 850 of file stl_list.h.

void list::merge ( list &&  __x)

Merge sorted lists.

Parameters:
xSorted 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.

Definition at line 289 of file list.tcc.

References std::begin(), std::end(), begin(), and end().

Referenced by sort().

template<typename _StrictWeakOrdering >
void list::merge ( list &&  __x,
_StrictWeakOrdering  __comp 
)

Merge sorted lists according to comparison function.

Parameters:
xSorted list to merge.
StrictWeakOrderingComparison function defining sort order.

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

Definition at line 323 of file list.tcc.

References std::begin(), std::end(), begin(), and end().

list< _Tp, _Alloc > & list::operator= ( const list __x)

List assignment operator.

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.

Parameters:
xA list of identical element and allocator types.

All the elements of x are copied, but unlike the copy constructor, the allocator object is not copied.

Definition at line 186 of file list.tcc.

References std::begin(), std::end(), begin(), and end().

list& std::list::operator= ( list &&  __x) [inline]

List move assignment operator.

Parameters:
xA list of identical element and allocator types.

The contents of x are moved into this list (without copying). x is a valid, but unspecified list

Definition at line 647 of file stl_list.h.

list& std::list::operator= ( initializer_list< value_type >  __l) [inline]

List initializer list assignment operator.

Parameters:
lAn initializer_list of value_type.

Replace the contents of the list with copies of the elements in the initializer_list l. This is linear in l.size().

Definition at line 664 of file stl_list.h.

void std::list::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 1013 of file stl_list.h.

void std::list::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 973 of file stl_list.h.

References std::begin().

void std::list::push_back ( const value_type &  __x) [inline]

Add data to the end of the list.

Parameters:
xData to be added.

This is a typical stack operation. The function creates an element at the end 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.

Definition at line 987 of file stl_list.h.

References std::end().

void std::list::push_front ( const value_type &  __x) [inline]

Add data to the front of the list.

Parameters:
xData 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.

Definition at line 946 of file stl_list.h.

References std::begin().

reverse_iterator std::list::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 766 of file stl_list.h.

References std::end().

const_reverse_iterator std::list::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 775 of file stl_list.h.

References std::end().

void list::remove ( const _Tp &  __value)

Remove all elements equal to value.

Parameters:
valueThe 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.

Definition at line 240 of file list.tcc.

References std::begin(), and std::end().

template<typename _Predicate >
void list::remove_if ( _Predicate  __pred)

Remove all elements satisfying a predicate.

Parameters:
PredicateUnary 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.

Definition at line 393 of file list.tcc.

References std::begin(), and std::end().

reverse_iterator std::list::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 784 of file stl_list.h.

References std::begin().

const_reverse_iterator std::list::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 793 of file stl_list.h.

References std::begin().

void list::resize ( size_type  __new_size)

Resizes the list to the specified number of elements.

Parameters:
new_sizeNumber 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.

Definition at line 140 of file list.tcc.

References std::begin(), and std::end().

void list::resize ( size_type  __new_size,
const value_type &  __x 
)

Resizes the list to the specified number of elements.

Parameters:
new_sizeNumber of elements the list should contain.
xData 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.

Definition at line 155 of file list.tcc.

References std::begin(), and std::end().

void std::list::reverse ( ) [inline]

Reverse the elements in list.

Reverse the order of elements in the list in linear time.

Definition at line 1415 of file stl_list.h.

size_type std::list::size ( ) const [inline]

Returns the number of elements in the list.

Definition at line 845 of file stl_list.h.

References std::distance(), std::begin(), and std::end().

void list::sort ( )

Sort the elements.

Sorts the elements of this list in NlogN time. Equivalent elements remain in list order.

Definition at line 355 of file list.tcc.

References splice(), begin(), std::begin(), empty(), merge(), and swap().

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.

Definition at line 432 of file list.tcc.

References splice(), begin(), std::begin(), empty(), merge(), and swap().

void std::list::splice ( iterator  __position,
list &&  __x 
) [inline]

Insert contents of another list.

Parameters:
positionIterator referencing the element to insert before.
xSource list.

The elements of x are inserted in constant time in front of the element referenced by position. x becomes an empty list.

Requires this != x.

Definition at line 1215 of file stl_list.h.

Referenced by sort().

void std::list::splice ( iterator  __position,
list &&  __x,
iterator  __i 
) [inline]

Insert element from another list.

Parameters:
positionIterator referencing the element to insert before.
xSource list.
iIterator referencing the element to move.

Removes the element in list x referenced by i and inserts it into the current list before position.

Definition at line 1245 of file stl_list.h.

void std::list::splice ( iterator  __position,
list &&  __x,
iterator  __first,
iterator  __last 
) [inline]

Insert range from another list.

Parameters:
positionIterator referencing the element to insert before.
xSource list.
firstIterator referencing the start of range in x.
lastIterator referencing the end of range in x.

Removes elements in the range [first,last) and inserts them before position in constant time.

Undefined if position is in [first,last).

Definition at line 1281 of file stl_list.h.

void std::list::swap ( list __x) [inline]

Swaps data with another list.

Parameters:
xA 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.

Definition at line 1177 of file stl_list.h.

Referenced by sort().

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 268 of file list.tcc.

References std::begin(), and std::end().

template<typename _BinaryPredicate >
void list::unique ( _BinaryPredicate  __binary_pred)

Remove consecutive elements satisfying a predicate.

Parameters:
BinaryPredicateBinary 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.

Definition at line 411 of file list.tcc.

References std::begin(), and std::end().


The documentation for this class was generated from the following files: