C++ Standard Library C++ STL Library

C++ - <list>



C++ <list>

A list is a sequence container that stores data of same type. List container is implemented as doubly-linked lists, hence provides sequential access of its data in both direction. It also allow constant time insertion and deletion operations anywhere within the sequence and in both direction.

The List supports shrink and expand functionality at runtime, hence, list requires more memory and in return it manage memory dynamically and efficiently.

The main disadvantage of using list and forward_list as compared to other sequence container is that they lack direct access of any elements by its position, for example - using operator []. To access any element in a list, iteration need to be done from a known position (like beginning or end of the list) to the element's position, which takes linear time to access element.

Syntax

template < class T, class Alloc = allocator<T> > class list; 

Parameters

T Type of the elements stored in the container.
Alloc Type of the allocator object used to define the storage allocation model. default: allocator.

Member Types

Member typesDefinition
value_type T (First template parameter)
allocator_type Alloc (Second template parameter), default: allocator<value_type>
reference value_type&
const_reference const value_type&
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a bidirectional iterator to value_type
const_iterator a bidirectional iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ <list> - Member Functions

The C++ list container has a number of member functions which are listed below:

FunctionsDescription
list() Construct a list object.
~list() Destroys container by deallocating container memory.
operator=() Assign content to a list.

Capacity

FunctionsDescription
empty() Checks whether the list is empty or not.
size() Returns the length of the list in terms of bytes.
max_size() Returns the maximum length of the list.

Element Access

FunctionsDescription
front() Access first element of the list.
back() Access last element of the list.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the list.
end() Returns iterator pointing to the past-the-last element of the list.
rbegin() Returns reverse iterator to the last element of the list.
rend() Returns reverse iterator to the element preceding the first element of the list.
cbegin() Returns const_iterator pointing to the first element of the list.
cend() Returns const_iterator pointing to the past-the-last element of the list.
crbegin() Returns const_reverse_iterator to the last element of the list.
crend() Returns const_reverse_iterator to the element preceding the first element of the list.

Modifiers

FunctionsDescription
assign() Assign list content.
clear() Clears all elements of the list.
pop_back() Deletes last element of the list.
push_back() Adds a new element at the end of the list.
pop_front() Deletes first element of the list.
push_front() Adds a new element at the beginning of the list.
insert() Insert elements in the list.
resize() Changes the size of the list by specified number of elements.
erase() Deletes either a single element or range of elements from a list.
swap() Exchanges elements between two lists.
emplace() Constructs and inserts a new element at specified position in the list
emplace_front() Constructs and inserts a new element at the beginning of the list.
emplace_back() Constructs and inserts a new element at the end of the list.

Operations

FunctionsDescription
merge() Merge two sorted lists into one.
remove() Delete all occurrences of specified element from the list.
remove_if() Delete all elements from a list based on specified condition.
reverse() Reverse the order of elements of the list container.
sort() Sorts all elements of a list.
splice() Transfer all elements from a list to *this.
unique() Deletes all consecutive duplicate elements from a list.

Allocator

FunctionsDescription
get_allocator() Return a copy of allocator object associated with the list.

C++ <list> - Non-member Functions

FunctionsDescription
operator == Checks whether two lists are equal or not.
operator != Checks whether two lists are unequal or not.
operator < Checks whether the first list is less than the other or not.
operator > Checks whether the first list is greater than the other or not.
operator <= Checks whether the first list is less than or equal to the other or not.
operator >= Checks whether the first list is greater than or equal to the other or not.
swap() Exchanges elements between two lists.