Standard librady header <list>

This header is part of the containers library.

list

  • Defined in header <list>
template<
  class T,
  class Allocator = std::allocator
> class list;

// Since c++17
namespace pmr {
  template< class T >
  using list = std:list<T, std::pmr::polymorhic_allocator<T>>;
}

std::list is a container that supports constant time insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is usually implemented as a doubly-linked list. Compared to std::forward_list this container provides bidirectional iteration capability while being less space efficient.

Removing and moving the elements within the list or across several lists does not invalidate the iterators or references. An iterator is invalidated only when the corresponding element is deleted.

// Since c++26 All member functions of std::list are constexpr: it is possible to create and use std::list objects in the evaluation of a constant expression. However, std::list objects generally cannot be constexpr, because any dynamically allocated storage must be released in the same evaluation of constant expression.

  • Template parameters
parameter definition
T The type of the elemets T must meet the requirements of CopyConstructible. T must meet the requirements of CopyAssignable if list::operator= or list::assign is instantiated with T. (until c++11)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since ++11, until c++17)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type meets the requirements of Erasable, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements. (since c++17)
Allocator An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined(until C++20)The program is ill-formed(since C++20) if Allocator::value_type is not the same as T.
  • Member types
Member type Definition
value_type T
allocator_type Allocator
size_type Unsigned integer type (usually std::size_t)
difference_type Signed integer type (usually std::ptrdiff_t)
reference value_type&
const_reference const value_type&
pointer Allocator::pointer (until c++11)
std::allocator_traits::pointer (since c++11)
const_pointer Allocator::pointer (until c++11)
std::allocator_traits::pointer (since c++11)
iterator LegacyBidirectionalIterator and ConstexprIterator(since C++26) to value_type
const_iterator LegacyBidirectionalIterator and ConstexprIterator(since C++26) to const value_type
reverse_iterator std::reverse_iterator
const_reverse_iterator std::reverse_iterator<const_iterator>
  • Member functions
Function Definition
(constructor) constructor the list
(destructor) destructs the list
operator= assigns values to the container
assign assigns values to the container
assign_range(c++23) assigns a range of values to the container
get_allocator returns the associated allocator
front access the first element
back access the last element
begin/cbegin(c++11) returns an iterator to the beginning
end/cend(c++11) returns an iterator to the end
rbegin/crbegin(c++11) returns a reverse iterator to the beginning
rend/crend(c++11) returns a reverse iterator to the end
empty checks whether the container is empty
size returns the number of elements
max_size returns the maximum possible number of elements
clear clears the contents
insert inserts elements
insert_range(c++23) inserts a range of elements
emplace (c++11) constructs element in-place
erase erases elements
push_back adds an element to the end
emplace_back(c++11) constructs an element in-place at the end
append_range(c++23) adds a range of elements to the end
pop_back removes the last element
push_front inserts an element to the beginning
emplace_front(c++11) construct an element in-place at the beginning
prepend_range(c++23) adds a range of elements to the beginning
pop_front removes the first element
resize changes the number of elements stored
swap swaps the contents
merge merges two sorted lists
splice transfers elements from another list
remove/remove_if removes elements satisfying specific criteria
reverse reverses the oder of the elements
unique removes consecutive duplicate elements
sort sorts the elements
  • Non-member functions
function Definition
operator== lexicographically compares the values of two lists
operaypr!= lexicographically compares the values of two lists
operaypr< lexicographically compares the values of two lists
operaypr<= lexicographically compares the values of two lists
operaypr> lexicographically compares the values of two lists
operaypr>= lexicographically compares the values of two lists
operaypr<=> lexicographically compares the values of two lists
std::swap specializes the std::swap algorithm
erase/erase_if(c++20) erases all elements satisfying specific criteria
  • Example
#include <algorithm>
#include <iostream>
#include <list>

int main() {
  // Create a list containing integers
  std::list<int> l = {7, 5, 16, 7};

  // Add an integer to the front of the list
  l.push_front(25);
  // Add an integer to the back of the list
  l.push_back(13);

  // Insert an integer before by searching
  auto it = std::find(l.begin(), l.end(), 16);
  if (it != l.end()) {
    l.insert(it, 42);
  }

  std::cout << "l = { ";
  for (int n : l) {
    std::cout << n << ", ";
  }
  std::cout << "};\n";
}