std::forward_list

template<
  class T,
  class Allocator = std::allocator<T>
> class forward_list;

namespace pmr {
  template< class T >
  using forward_list = std::forward_list<T, std::pmr::polymorphic_allocator<T>>;
}
  • Definition

std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is implemented as a singly-linked list. Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed.

Adding, removing and moving the elements within the list, or across several lists, does not invalidate the iterators currently referring to other elements in the list. However, an iterator or reference referring to an element is invalidated when the corresponding element is removed (via erase_after) from the list.

std::forward_list meets the requirements of Container (except for the size member function and that operator=='s complexity is always linear), AllocatorAwareContainer and SequenceContainer.

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

Template parameters

  • T: The type of the elements.

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. (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 std::allocator_traits::pointer
const_pointer std::allocator_traits::const_pointer
iterator LegacyForwardIterator and ConstexprIterator(since C++26) to value_type
const_iterator LegacyForwardIterator and ConstexprIterator(since C++26) to const value_type

Member functions

Function Definition
(constructor) constructs the forward_list
(destructor) destructs the forward_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
before_begin/cbefore_begin returns an iterator o the element before beginning
begin/cbegin returns an iterator to the beginning
end/cend returns an iterator to the end
empty checks whether the container is empty
max_size returns the maximum possible number of elements
clear clears the contents
insert_after inserts elements after an element
emplace_after constructs elements in-place after an element
insert_range_after (c++23) inserts an element after an element
erase_after erases an element after an element
push_front inserts an element to the beginning
emplace_front constructs an element in-place at the beginning
prepend_range (c++23) adds a rabfe if 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_after transfers elements from another forward_list
remove/remove_if removes elements satisfying specific criteria
reverse reverses the order of the elements
unique removes consecutive duplicate elements
sort sorts the elements

Non-member functions

Function Definition
operator== (C++11) lexicographically compares the value of two forward_lists
operator!= (C++11)(removed in C++20) lexicographically compares the value of two forward_lists
operator< (C++11)(removed in C++20) lexicographically compares the value of two forward_lists
operator<= (C++11)(removed in C++20) lexicographically compares the value of two forward_lists
operator> (C++11)(removed in C++20) lexicographically compares the value of two forward_lists
operator>= (C++11)(removed in C++20) lexicographically compares the value of two forward_lists
operator<=> (C++20) lexicographically compares the value of two forward_lists

Example

#include <forward_list>
#include <iostream>

int main() {

  std::forward_list<int> l = {1, 2, 3, 4};

  l.push_front(0);

  l.insert_after(l.begin(), 5);

  for (auto it = l.begin(); it != l.end(); ++it) {
    std::cout << *it << std::endl;
  }

  return 0;
}

/**
 * 0
   5
   1
   2
   3
   4
 */

Reference

1. cppreference.com