C++ Library - <forward_list>



Introduction

forward_list is a popularly used sequence container. Container is an object that holds data of same type. forward_list container is implemented as singly linked-list, hence it provides unidirectional sequential access to it's data.

forward_list doesn't provide fast random access, it only supports sequential access in only one directions. forward_list allows insertion and deletion operation anywhere within a sequence in constant time.

Elements of forward_list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it's data. forward_lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator.

Zero sized forward_lists are also valid. In that case forward_list.begin() and forward_list.end() points to same location. But behavior of calling front() is undefined.

Definition

Below is definition of std::forward_list from <forward_list> header file

template < class T, class Alloc = allocator<T> > class forward_list;

Parameters

  • T − Type of the element contained.

    T may be substituted by any other data type including user-defined type.

  • Alloc − Type of allocator object.

    By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.

Member types

Following member types can be used as parameters or return type by member functions.

Sr.No.Member typesDefinition
1value_typeT (First parameter of the template)
2allocator_typeAlloc (Second parameter of the template)
3referencevalue_type&
4const_referenceconst value_type&
5pointervalue_type*
6const_pointerconst value_type*
7iteratora random access iterator to value_type
8const_iteratora random access iterator to const value_type
9size_typesize_t
10difference_typeptrdiff_t

Functions from <forward_list>

Below is list of all methods from <forward_list> header.

Constructors

Sr.No.Method & Description
1forward_list::forward_list default constructor

Constructs an empty forward_list with zero elements.

2forward_list::forward_list fill constructor

Constructs a new forward_list with n elements and assigns val to each element of forward_list.

3forward_list::forward_list fill constructor

Constructs a new forward_list with n elements and assign zero value to each element of forward_list.

4forward_list::forward_list range constructor

Constructs a forward_list with as many elements as in range of first to last.

5forward_list::forward_list copy constructor

Constructs a forward_list with copy of each elements present in existing list.

6forward_list::forward_list move constructor

Constructs a forward_list with the contents of other using move semantics.

7forward_list::forward_list initializer list constructor

Constructs a forward_list from initialize list.

Destructor

Sr.No.Method & Description
1forward_list::~forward_list

Destroys forward_list object by deallocating it's memory.

Member functions

Sr.No.Method & Description
1forward_list::assign range version

Assigns new value to forward_list by replacing old ones.

2forward_list::assign fill version

Assigns new values to forward_list by replacing old ones.

3forward_list::assign initializer list version

Assigns new values to forward_list by replacing old ones.

4forward_list::before_begin

Returns a random access iterator which points to the position before the first element of the forward_list.

5forward_list::begin

Returns a random access iterator which points to the first element of the forward_list.

6forward_list::cbefore_begin

Returns a constant random access iterator which points to the position before the first element of the forward_list.

7forward_list::cbegin

Returns a constant random access iterator which points to the beginning of the forward_list.

8forward_list::cend

Returns a constant random access iterator which points to the end of the forward_list.

9forward_list::clear

Destroys the forward_list by removing all elements from the forward_list and sets size of forward_list to zero.

10forward_list::emplace_after

Constructs and inserts new element after position in the forward_list and increases size of forward_list by one.

11forward_list::emplace_front

Inserts new element at the beginning of the forward_list and increases size of forward_list by one.

12forward_list::empty

Tests whether forward_list is empty or not.

13forward_list::end

Returns a random access iterator which points to the last element of the forward_list.

14forward_list::erase_after position version

Removes single element after position from the the forward_list.

15forward_list::erase range version

Removes range of element from the the forward_list.

16forward_list::front

Returns a reference to the first element of the forward_list.

17forward_list::get_allocator

Returns an allocator associated with forward_list

18forward_list::insert_after single element version

Extends iterator by inserting new element after position in forward_list

19forward_list::insert_after move version

Extends iterator by inserting new element after position in forward_list

20forward_list::insert_after fill version

Extends iterator by inserting new element after position in forward_list

21forward_list::insert_after range version

Extends iterator by inserting new element after position in forward_list

22forward_list::insert_after initializer list version

Extends iterator by inserting new element after position in forward_list

23forward_list::max_size

Returns the maximum number of elements can be held by forward_list.

24forward_list::merge

Merges two sorted forward_lists into one.

25forward_list::merge move version

Merges two sorted forward_lists into one.

26forward_list::merge compare function

Merges two sorted forward_lists into one.

27forward_list::merge compare function move version

Merges two sorted forward_lists into one by using move semantics.

28forward_list::operator= copy version

Assigns new contents to the forward_list by replacing old ones.

29forward_list::operator= move version

Assign new contents to the forward_list by replacing old ones.

30forward_list::operator= initializer list version

Assign new contents to the forward_list by replacing old ones.

31forward_list::pop_front

Removes first element from forward_list.

32forward_list::push_front copy version

Inserts new element at the beginning of forward_list.

33forward_list::push_front move version

Inserts new element at the beginning of forward_list.

34forward_list::remove

removes element(s) from the forward_list that matches the value.

35forward_list::remove_if

removes elements from the forward_list that fulfills the condition.

36forward_list::resize

Changes the size of forward_list.

37forward_list::resize value version

Changes the size of forward_list.

38forward_list::reverse

Reverses the order of the elements present in the forward_list.

39forward_list::sort

Sorts the elements of the forward_list.

40forward_list::sort compare function

Sorts the elements of the forward_list.

41forward_list::splice_after

Transfers all elements from forward_list to *this.

42forward_list::splice_after move version

Transfers all elements from forward_list x to *this by using move semantics.

43forward_list::splice_after single element

Transfers a element pointed to by iterator i from forward_list x into *this.

44forward_list::splice_after single element move version

Transfers the element pointed to by iterator i from forward_list x into *this by using move semantics.

45forward_list::splice_after range version

Transfers the elements in the range of first to last from x to *this.

46forward_list::splice range and move version

Transfers the elements in the range of first to last from x to *this by using move semantics.

47forward_list::swap

Exchanges the content of a forward_list with contents of another forward_list x.

48forward_list::unique

Removes all consecutive duplicate elements from the forward_list.

49forward_list::unique

Removes all consecutive duplicate elements from the forward_list.

Non-member overloaded functions

Sr.No.Method & Description
1operator==

Tests whether two forward_lists are equal or not.

2operator!=

Tests whether two forward_lists are equal or not.

3operator<

Tests whether first forward_list is less than other or not.

4operator<=

Tests whether first forward_list is less than or equal to other or not.

5operator>

Tests whether first forward_list is greater than other or not.

6operator>=

Tests whether first forward_list is greater than or equal to other or not.

7swap

Exchanges the contents of two forward_list.