Skip to main content

Defined in: <forward_list>

caution

Note, this article is not finished! You can help by editing this doc page.

Overview

template< class T, /* .. */ >
class forward_list;

Forward list is a container that supports fast insertion and removal of elements from anywhere in the container.

Technical definition of a forward list

std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container.

important

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.

std::forward_list

Defined inforward_list

Template parameters

See technical details for more detailed information.

pubTType of the elements.
pubAllocator

Allocator that is used to acquire/release memory and to construct/destroy the elements in that memory.

Type names

pubvalue_typeT
puballocator_typeAllocator
pubsize_typeUnsigned integer type (usually std::size_t)
pubdifference_typeSigned integer type (usually std::ptrdiff_t)
pubreferencevalue_type&
pubconst_referencevalue_type const&
pubpointerAllocator::pointer (until C++11)
std::allocator_traits<Allocator>::pointer (since C++11)
pubconst_pointerAllocator::const_pointer (until C++11)
std::allocator_traits<Allocator>::const_pointer (since C++11)
pubiterator

LegacyForwardIterator to value_type 

pubconst_iterator

LegacyForwardIterator to const value_type 

Member functions

pub(constructors)

Constructs a vector.

pub(destructor)

Destroys the vector, deallocating internal storage if used.

puboperator=

Assigns values to the container.

pubassign

Assigns values to the container.

pubget_allocator

Returns the associated allocator.

Element access

pubfront

Access the first element.

Iterators

pubbefore_begin
cbefore_begin

Returns an iterator/const_iterator to the element before the beginning.

pubbegin
cbegin

Returns an iterator/const_iterator to the beginning.

pubend
cend

Returns an iterator/const_iterator to the end.

Capacity

pubempty

Returns true if the list is empty, otherwise false.

pubmax_size

Returns the maximum possible number of elements.

Modifiers

pubclear

Clears the contents.

pubinsert_after

Inserts elements after the elements.

pubemplace_after

Constructs elements in-place after an element.

puberase_after

Erases an element after an element.

pubpush_front

Inserts an element to the beginning.

pubemplace_front

Constructs an element in-place at the beginning.

pubpop_front

Removes the first element.

pubresize

Changes the number of elements stored.

pubswap

Swaps the contents.

Operations

pubmerge

Merges two sorted lists.

pubsplice_after

Moves elements from another forward list.

pubremove
remove_if

Removes elements satisfying specific criteria.

pubreverse

Reverses the order of the elements.

pubunique

Removes consecutive duplicate elements.

pubsort

Sorts the elements.

Non-member functions

puboperator==
operator!= (removed in C++20)
operator< (removed in C++20)
operator> (removed in C++20)
operator<= (removed in C++20)
operator>= (removed in C++20)
operator<=> (since C++20)
Lexicographically compares the values in the list.
pubstd::swap (std::forward_list)An overload for a std::swap algorithm.
puberase (std::forward_list)
erase_if (std::forward_list)
Overloads for std::erase/std::erase_if algorithms.

Deduction guides (since C++17)

Click to expand

Deduction guides

// (1)
template< class InputIt,
class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
forward_list(InputIt, InputIt, Alloc = Alloc())
-> forward_list<typename std::iterator_traits<InputIt>::value_type, Alloc>;

(1) allows deduction from an iterator range.

Overload resolution

In order for any of the deduction guides to participate in overload resolution, the folllowing requirements must be met:

note

The extent to which the library determines that a type does not satisfy LegacyInputIterator is unspecified, except that as a minimum:

  • Integral types do not qualify as input iterators.

Likewise, the extent to which it determines that a type does not satisfy Allocator is unspecified, except that as a minimum:

Examples

important

This section requires improvement. You can help by editing this doc page.

This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

Defined in: <forward_list>

caution

Note, this article is not finished! You can help by editing this doc page.

Overview

template< class T, /* .. */ >
class forward_list;

Forward list is a container that supports fast insertion and removal of elements from anywhere in the container.

Technical definition of a forward list

std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container.

important

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.

std::forward_list

Defined inforward_list

Template parameters

See technical details for more detailed information.

pubTType of the elements.
pubAllocator

Allocator that is used to acquire/release memory and to construct/destroy the elements in that memory.

Type names

pubvalue_typeT
puballocator_typeAllocator
pubsize_typeUnsigned integer type (usually std::size_t)
pubdifference_typeSigned integer type (usually std::ptrdiff_t)
pubreferencevalue_type&
pubconst_referencevalue_type const&
pubpointerAllocator::pointer (until C++11)
std::allocator_traits<Allocator>::pointer (since C++11)
pubconst_pointerAllocator::const_pointer (until C++11)
std::allocator_traits<Allocator>::const_pointer (since C++11)
pubiterator

LegacyForwardIterator to value_type 

pubconst_iterator

LegacyForwardIterator to const value_type 

Member functions

pub(constructors)

Constructs a vector.

pub(destructor)

Destroys the vector, deallocating internal storage if used.

puboperator=

Assigns values to the container.

pubassign

Assigns values to the container.

pubget_allocator

Returns the associated allocator.

Element access

pubfront

Access the first element.

Iterators

pubbefore_begin
cbefore_begin

Returns an iterator/const_iterator to the element before the beginning.

pubbegin
cbegin

Returns an iterator/const_iterator to the beginning.

pubend
cend

Returns an iterator/const_iterator to the end.

Capacity

pubempty

Returns true if the list is empty, otherwise false.

pubmax_size

Returns the maximum possible number of elements.

Modifiers

pubclear

Clears the contents.

pubinsert_after

Inserts elements after the elements.

pubemplace_after

Constructs elements in-place after an element.

puberase_after

Erases an element after an element.

pubpush_front

Inserts an element to the beginning.

pubemplace_front

Constructs an element in-place at the beginning.

pubpop_front

Removes the first element.

pubresize

Changes the number of elements stored.

pubswap

Swaps the contents.

Operations

pubmerge

Merges two sorted lists.

pubsplice_after

Moves elements from another forward list.

pubremove
remove_if

Removes elements satisfying specific criteria.

pubreverse

Reverses the order of the elements.

pubunique

Removes consecutive duplicate elements.

pubsort

Sorts the elements.

Non-member functions

puboperator==
operator!= (removed in C++20)
operator< (removed in C++20)
operator> (removed in C++20)
operator<= (removed in C++20)
operator>= (removed in C++20)
operator<=> (since C++20)
Lexicographically compares the values in the list.
pubstd::swap (std::forward_list)An overload for a std::swap algorithm.
puberase (std::forward_list)
erase_if (std::forward_list)
Overloads for std::erase/std::erase_if algorithms.

Deduction guides (since C++17)

Click to expand

Deduction guides

// (1)
template< class InputIt,
class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
forward_list(InputIt, InputIt, Alloc = Alloc())
-> forward_list<typename std::iterator_traits<InputIt>::value_type, Alloc>;

(1) allows deduction from an iterator range.

Overload resolution

In order for any of the deduction guides to participate in overload resolution, the folllowing requirements must be met:

note

The extent to which the library determines that a type does not satisfy LegacyInputIterator is unspecified, except that as a minimum:

  • Integral types do not qualify as input iterators.

Likewise, the extent to which it determines that a type does not satisfy Allocator is unspecified, except that as a minimum:

Examples

important

This section requires improvement. You can help by editing this doc page.

This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.