Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


C++ <forward_list>

A forward list is a sequence container that stores data of same type. List container is implemented as singly-linked lists, hence provides sequential access of its data. It also allow constant time insertion and deletion operations anywhere within the sequence. Please note that, forward_list can only be iterated forwards.

The forward_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 forward_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 forward_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 forward iterator to const value_type, convertible to const_iterator
const_iterator a forward iterator to const value_type
difference_type ptrdiff_t
size_type size_t

C++ <forward_list> - Member Functions

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

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

Capacity

FunctionsDescription
empty() Checks whether the forward_list is empty or not.
max_size() Returns the maximum length of the forward_list.

Element Access

FunctionsDescription
front() Access first element of the forward_list.

Iterators

FunctionsDescription
before_begin() Returns iterator pointing to the before-the-first element of the forward_list.
begin() Returns iterator pointing to the first element of the forward_list.
end() Returns iterator pointing to the past-the-last element of the forward_list.
cbefore_begin() Returns const_iterator pointing to the before-the-first element of the forward_list.
cbegin() Returns const_iterator pointing to the first element of the forward_list.
cend() Returns const_iterator pointing to the past-the-last element of the forward_list.

Modifiers

FunctionsDescription
assign() Assign forward_list content.
clear() Clears all elements of the forward_list.
pop_front() Deletes first element of the forward_list.
push_front() Adds a new element at the beginning of the forward_list.
emplace_front() Constructs and inserts a new element at the beginning of the forward_list.
emplace_after() Constructs and inserts a new element at the specified position in the forward_list.
erase_after() Deletes an element or range of elements from the forward_list.
insert_after() Inserts a new element or range of elements in the forward_list.
resize() Changes the size of the forward_list by specified number of elements.
swap() Exchanges elements between two forward_lists.

Operations

FunctionsDescription
merge() Merge two sorted forward_lists into one.
splice_after() Transfers elements from one forward_list to another forward_list.
remove() Delete all occurrences of specified element from the forward_list.
remove_if() Delete all elements from a forward_list based on specified condition.
reverse() Reverse the order of elements of the forward_list container.
sort() Sorts all elements of a forward_list.
unique() Deletes all consecutive duplicate elements from a forward_list.

Allocator

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

C++ <forward_list> - Non-member Functions

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

5