From cppreference.com
Defined in header
template<classT,classAllocator=std::allocator<T>>classforward_list; (1) (since C++11)namespacepmr{template<classT>usingforward_list=std::forward_list<T,std::pmr::polymorphic_allocator<T>>;} (2) (since C++17)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
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
) from the list.
std::forward_list meets the requirements of
(except for the size member function and that operator=='s complexity is always linear),
and
.
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.
However, defining a constexprstd::forward_list variable is generally an error, because constant evaluation requires any dynamically allocated storage to be released in the same evaluation, which is usually not the case with std::forward_list's initializer.
(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
, 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
, but many member functions impose stricter requirements. This container (but not its members) can be instantiated with an
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
. 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_typeT
allocator_typeAllocator
size_typeunsigned integer type (usually
)
difference_typesigned integer type (usually
)
referencevalue_type&
const_referenceconstvalue_type&
pointerstd::allocator_traits<Allocator>::pointer
const_pointerstd::allocator_traits<Allocator>::const_pointer
iterator
and
(since C++26) to value_type
const_iterator
and
(since C++26) to constvalue_type
Member functions
constructs the forward_list
(public member function)
destructs the forward_list
(public member function)
assigns values to the container
(public member function)
assigns values to the container
(public member function)
(C++23)
assigns a range of values to the container
(public member function)
returns the associated allocator
(public member function)
Element access
access the first element
(public member function)
Iterators
returns an iterator to the element before beginning
(public member function)
returns an iterator to the beginning
(public member function)
returns an iterator to the end
(public member function)
Capacity
checks whether the container is empty
(public member function)
returns the maximum possible number of elements
(public member function)
Modifiers
clears the contents
(public member function)
inserts elements after an element
(public member function)
constructs elements in-place after an element
(public member function)
(C++23)
inserts a range of elements after an element
(public member function)
erases an element after an element
(public member function)
inserts an element to the beginning
(public member function)
constructs an element in-place at the beginning
(public member function)
(C++23)
adds a range of elements to the beginning
(public member function)
removes the first element
(public member function)
changes the number of elements stored
(public member function)
swaps the contents
(public member function)
Operations
merges two sorted lists
(public member function)
moves elements from another forward_list
(public member function)
removes elements satisfying specific criteria
(public member function)
reverses the order of the elements
(public member function)
removes consecutive duplicate elements
(public member function)
sorts the elements
(public member function)
Non-member functions
Notes
macro ValueStdFeature
__cpp_lib_incomplete_container_elements
(C++17)Minimal incomplete type support
(C++23)Ranges construction and insertion for containers
__cpp_lib_constexpr_forward_list
(C++26)Constexpr std::forward_listExample
See also