# list Class Template Reference
[Containers, Sequences]

List of all members.

## Detailed Description

### template<typename _Tp, typename _Alloc = allocator<_Tp>>
class std::list< _Tp, _Alloc >

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.

Definition at line 391 of file stl_list.h.

## Public Types

- typedef _Base::allocator_type allocator_type
- typedef _List_const_iterator<
  _Tp > const_iterator
- typedef _Alloc::const_pointer const_pointer
- typedef _Alloc::const_reference const_reference
- typedef std::reverse_iterator<
  const_iterator > const_reverse_iterator
- typedef ptrdiff_t difference_type
- typedef _List_iterator< _Tp > iterator
- typedef _Alloc::pointer pointer
- typedef _Alloc::reference reference
- typedef std::reverse_iterator<
  iterator > reverse_iterator
- typedef size_t size_type
- typedef _Tp value_type

## Public Member Functions

- template<typename _InputIterator> void assign (_InputIterator __first, _InputIterator __last)
- void assign (size_type __n, const value_type &__val)
- const_reference back () const
- reference back ()
- const_iterator begin () const
- iterator begin ()
- void clear ()
- bool empty () const
- const_iterator end () const
- iterator end ()
- iterator erase (iterator __first, iterator __last)
- iterator erase (iterator __position)
- const_reference front () const
- reference front ()
- allocator_type get_allocator () const
- template<typename _InputIterator> void insert (iterator __position, _InputIterator __first, _InputIterator __last)
- void insert (iterator __position, size_type __n, const value_type &__x)
- iterator insert (iterator __position, const value_type &__x)
- template<typename _InputIterator> list (_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
- list (const list &__x)
- list (size_type __n)
- list (size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
- list (const allocator_type &__a=allocator_type())
- size_type max_size () const
- template<typename _StrictWeakOrdering> void merge (list &, _StrictWeakOrdering)
- void merge (list &__x)
- list & operator= (const list &__x)
- void pop_back ()
- void pop_front ()
- void push_back (const value_type &__x)
- void push_front (const value_type &__x)
- const_reverse_iterator rbegin () const
- reverse_iterator rbegin ()
- void remove (const _Tp &__value)
- template<typename _Predicate> void remove_if (_Predicate)
- const_reverse_iterator rend () const
- reverse_iterator rend ()
- void resize (size_type __new_size)
- void resize (size_type __new_size, const value_type &__x)
- void reverse ()
- size_type size () const
- template<typename _StrictWeakOrdering> void sort (_StrictWeakOrdering)
- void sort ()
- void splice (iterator __position, list &, iterator __first, iterator __last)
- void splice (iterator __position, list &, iterator __i)
- void splice (iterator __position, list &__x)
- void swap (list &__x)
- template<typename _BinaryPredicate> void unique (_BinaryPredicate)
- void unique ()

## Protected Types

- typedef _List_node< _Tp > _Node

## 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)
- _Node * _M_create_node (const value_type &__x)
- void _M_erase (iterator __position)
- void _M_fill_assign (size_type __n, const value_type &__val)
- void _M_fill_insert (iterator __pos, size_type __n, const value_type &__x)
- void _M_insert (iterator __position, const value_type &__x)
- template<typename _InputIterator> void _M_insert_dispatch (iterator __pos, _InputIterator __first, _InputIterator __last, __false_type)
- template<typename _Integer> void _M_insert_dispatch (iterator __pos, _Integer __n, _Integer __x, __true_type)
- void _M_transfer (iterator __position, iterator __first, iterator __last)

## Constructor & Destructor Documentation

list | ( | const allocator_type & | __a = `allocator_type()` | ) | `[inline, explicit]`

| Default constructor creates no elements. Definition at line 457 of file stl_list.h.

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

Create a list with copies of an exemplar element.

Parameters:

  | n | The number of elements to initially create.
  | value | An element to copy.

This constructor fills the list with n copies of value.

Definition at line 467 of file stl_list.h.

list | ( | size_type | __n | ) | `[inline, explicit]`

Create a list with default elements.

Parameters:

  | n | The number of elements to initially create.

This constructor fills the list with n copies of a default-constructed element.

Definition at line 480 of file stl_list.h.

list | ( | const list< _Tp, _Alloc > & | __x | ) | `[inline]`

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.

Definition at line 491 of file stl_list.h.

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

list | ( | _InputIterator | __first,
|  | _InputIterator | __last,
|  | const allocator_type & | __a = `allocator_type()`
| ) | `[inline]`

Builds a list from a range.

Parameters:

  | first | An input iterator.
  | last | An input iterator.

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 510 of file stl_list.h.

## Member Function Documentation

void assign | ( | _InputIterator | __first,
|  | _InputIterator | __last
| ) | `[inline]`

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. Old data may be lost.

Definition at line 561 of file stl_list.h.

void assign | ( | size_type | __n,
|  | const value_type & | __val
| ) | `[inline]`

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. Old data may be lost.

Definition at line 544 of file stl_list.h.

const_reference back | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reference to the data at the last element of the list. Definition at line 723 of file stl_list.h.

reference back | ( |  | ) | `[inline]`

| Returns a read/write reference to the data at the last element of the list. Definition at line 711 of file stl_list.h.

const_iterator 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 588 of file stl_list.h.

iterator 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 579 of file stl_list.h. Referenced by list::list(), list::merge(), list::operator=(), std::operator==(), list::remove_if(), list::resize(), list::sort(), list::splice(), and list::unique().

void 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 responsibilty. Definition at line 910 of file stl_list.h.

bool empty | ( |  | ) | const`[inline]`

| Returns true if the list is empty. (Thus begin() would equal end().) Definition at line 650 of file stl_list.h. Referenced by list::sort(), and list::splice().

const_iterator 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 605 of file stl_list.h.

iterator 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 597 of file stl_list.h. Referenced by list::list(), list::merge(), list::operator=(), std::operator==(), list::remove_if(), list::resize(), list::splice(), and list::unique().

iterator erase | ( | iterator | __first,
|  | iterator | __last
| ) | `[inline]`

Remove a range of elements.

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.

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

Definition at line 883 of file stl_list.h.

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

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

Definition at line 95 of file list.tcc.

References _List_iterator::_M_node.

Referenced by list::operator=(), and list::resize().

const_reference front | ( |  | ) | const`[inline]`

| Returns a read-only (constant) reference to the data at the first element of the list. Definition at line 703 of file stl_list.h.

reference front | ( |  | ) | `[inline]`

| Returns a read/write reference to the data at the first element of the list. Definition at line 695 of file stl_list.h.

allocator_type get_allocator | ( |  | ) | const`[inline]`

| Get a copy of the memory allocation object. Definition at line 570 of file stl_list.h.

void insert | ( | iterator | __position,
|  | _InputIterator | __first,
|  | _InputIterator | __last
| ) | `[inline]`

Inserts a range into the list.

Parameters:

  | position | An iterator into the list.
  | first | An input iterator.
  | last | An input iterator.

This function will insert copies of the data in the range [first,last) into the list before the location specified by position.

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 837 of file stl_list.h.

void insert | ( | iterator | __position,
|  | size_type | __n,
|  | const value_type & | __x
| ) | `[inline]`

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

Parameters:

  | position | An iterator into the list.
  | n | Number of elements to be inserted.
  | x | Data to be inserted.

This function will insert a specified number of copies of the given data before the location specified by position.

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 818 of file stl_list.h.

list< _Tp, _Alloc >::iterator insert | ( | iterator | __position,
|  | const value_type & | __x
| ) |

Inserts given value into list before 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 does not invalidate iterators and references.

Definition at line 85 of file list.tcc.

References _List_iterator::_M_node.

Referenced by list::operator=(), and list::resize().

size_type max_size | ( |  | ) | const`[inline]`

| Returns the size() of the largest possible list. Definition at line 660 of file stl_list.h.

void merge | ( | list< _Tp, _Alloc > & | ,
|  | _StrictWeakOrdering |
| ) |

Merge sorted lists according to comparison function.

Parameters:

  | x | Sorted list to merge.
  | StrictWeakOrdering | Comparison function definining 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 315 of file list.tcc.

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

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

Definition at line 211 of file list.tcc.

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

Referenced by list::sort().

list< _Tp, _Alloc > & 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, but unlike the copy constructor, the allocator object is not copied.

Definition at line 120 of file list.tcc.

References list::begin(), list::end(), list::erase(), and list::insert().

void 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 787 of file stl_list.h.

void 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 758 of file stl_list.h.

void push_back | ( | const value_type & | __x | ) | `[inline]`

Add data to the end of the list.

Parameters:

  | x | Data 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 772 of file stl_list.h.

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

Definition at line 742 of file stl_list.h.

const_reverse_iterator 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 623 of file stl_list.h.

reverse_iterator 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 614 of file stl_list.h.

void 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 responsibilty.

void remove_if | ( | _Predicate |  | ) |

Remove all elements satisfying a predicate.

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

Definition at line 276 of file list.tcc.

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

const_reverse_iterator 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 641 of file stl_list.h.

reverse_iterator 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 632 of file stl_list.h.

void resize | ( | size_type | __new_size | ) | `[inline]`

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 the list is extended and new elements are default-constructed.

Definition at line 686 of file stl_list.h.

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

Definition at line 105 of file list.tcc.

References list::begin(), list::end(), list::erase(), and list::insert().

void reverse | ( |  | ) | `[inline]`

| Reverse the elements in list. Reverse the order of elements in the list in linear time. Definition at line 1063 of file stl_list.h.

size_type size | ( |  | ) | const`[inline]`

| Returns the number of elements in the list. Definition at line 655 of file stl_list.h. References std::distance().

void 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 343 of file list.tcc. References list::begin(), list::empty(), list::merge(), list::splice(), and list::swap().

void sort | ( |  | ) |

| Sort the elements. Sorts the elements of this list in NlogN time. Equivalent elements remain in list order. Definition at line 238 of file list.tcc. References list::begin(), list::empty(), list::merge(), list::splice(), and list::swap().

void splice | ( | iterator | __position,
|  | list< _Tp, _Alloc > & | ,
|  | iterator | __first,
|  | iterator | __last
| ) | `[inline]`

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 position is in [first,last).

Definition at line 965 of file stl_list.h.

void splice | ( | iterator | __position,
|  | list< _Tp, _Alloc > & | ,
|  | iterator | __i
| ) | `[inline]`

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.

Definition at line 943 of file stl_list.h.

void splice | ( | iterator | __position,
|  | list< _Tp, _Alloc > & | __x
| ) | `[inline]`

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.

Definition at line 927 of file stl_list.h.

References list::begin(), list::empty(), and list::end().

Referenced by list::sort().

void swap | ( | list< _Tp, _Alloc > & | __x | ) | `[inline]`

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.

Definition at line 900 of file stl_list.h.

References std::swap().

Referenced by list::sort(), and std::swap().

void unique | ( | _BinaryPredicate |  | ) |

Remove consecutive elements satisfying a predicate.

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

Definition at line 294 of file list.tcc.

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

void 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 responsibilty.

Definition at line 191 of file list.tcc.

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

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

- stl_list.h
- list.tcc