How to find Size of std::forward_list in C++ STL Last Updated : 08 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Forward list in standard template library of C++. It comes under #include<forward_list> header file. It is implemented as a singly linked list. It was introduced in C++ 11 for the first time. Forward lists are sequence containers that allow constant time insert and erase operations from anywhere within the sequence. In the case of a forward list, fast random access is not supported. Unlike other STL libraries, std::forward_list does not have any size() method. Hence, in this article, we will show you how to get the size of a std::forward_list in C++ STL. There is a problem in retrieving the size of forward lists because std::forward_list doesn't have any std::size() member function. To get the size of forward lists, one can use std::distance() function. Approach: Since std::distance() function takes two iterators as arguments and it returns an integer, the std::begin() and std::end() function can be passed which points to the address of the first item and the address just after the last item. Syntax: size = distance(forward_list.begin(), forward_list.end()); Below is the C++ code to implement the above approach: C++14 // C++ program to implement // the above approach #include <forward_list> #include <iostream> using namespace std; // Driver code int main() { forward_list<int> l1 = { 3, 5, 6, 9, 6 }; // l.size() will throw an error, since // there is no size() method for // forward_list. So, to calculate the // size, we will use std::distance(iterator1, // iterator2), where iterator1 will be // l.begin() and iterator2 will be l.end() int size = distance(l1.begin(), l1.end()); cout << "Size of l1 is : " << size << endl; l1.remove(6); // It will erase all instances of 6 // from the list size = distance(l1.begin(), l1.end()); cout << "Size of l1, after removing all" << " instances of 6 is : " << size << endl; forward_list<int> l2 = { 6, 11, 0 }; int size2 = distance(l2.begin(), l2.end()); cout << "Size of l2, before assigning" << " it to l1 : " << size2 << endl; l1.splice_after(l1.begin(), l2); // It will assign l2 to l at the // provided iterator, making l1 // as empty. size = distance(l1.begin(), l1.end()); size2 = distance(l2.begin(), l2.end()); cout << "Size of l1, after assigning" << " l2 to it : " << size << endl; cout << "Size of l2, after assigning" << " it to l1 : " << size2 << endl; } Output: Size of l1 is : 5 Size of l1, after removing all instances of 6 is : 3 Size of l2, before assigning it to l1 : 3 Size of l1, after assigning l2 to it : 6 Size of l2, after assigning it to l1 : 0 Comment More infoAdvertise with us ashish0401 Follow Improve Article Tags : C++ Programs C++ C++ Quiz STL CPP-forward-list +1 More Practice Tags : CPPSTL Similar Reads Forward List in C++ STL In C++, forward_list container provides the implementation of singly linked list data structure. It stores data in non-contiguous memory where each element points to the next element in the sequence. This makes insertion and deletion faster once the position of the element is known.Example:C++#inclu 7 min read Commonly Used Methodsforward_list::begin() and forward_list::end() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, the forward list is more useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that the forward list 3 min read forward_list::push_front() and forward_list::pop_front() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of 4 min read forward_list assign() function in C++ STLThe forward_list::assign() is a function in C++ STL which assigns new content to a forward list, replacing its current content and adjusting its size as required.Syntax: Version 1:forward_list_name.assign(iterator it1, iterator it2) Version 2:forward_list_name.assign(int n, val) Version 3:forward_li 2 min read forward_list::front() and forward_list::empty() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of 3 min read forward_list::remove() and forward_list::remove_if() in C++ STLForward list in STL implements singly linked list. The forward list was introduced in C++11, and is useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from the list by the fact that the forward 4 min read forward_list::clear() and forward_list::erase_after() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of 4 min read forward_list::reverse() in C++ STLstd::forward_list::reverse() is an inbuilt function in CPP STL which reverses the order of the elements present in the forward_list. Syntax: forwardlist_name.reverse()Parameter: The function does not accept any parameter. Return value: The function has no return value. It reverses the forward list. 1 min read forward_list::swap() in C++ STLThe forward_list::swap() is a built-in function in CPP STL which exchanges the contents of the first given forward_list with another forward_list. Syntax: swap(forward_list first, forward_list second) or forward_list1.swap(forward_list second) Parameters: The function accepts two parameters which ar 3 min read std::forward_list::sort() in C++ STLForward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of 3 min read Other Member Methodsforward_list insert_after() function in C++ STLThe forward_list::insert_after() is a builtin function in C++ STL which gives us a choice to insert elements at the position just after the element pointed by a given iterator in the forward list. The arguments in this function are copied at the desired position. Syntax: forward_list_name.insert_aft 3 min read forward_list::unique() in C++ STLforward_list::unique() is an inbuilt function in C++ STL which removes all consecutive duplicate elements from the forward_list. It uses binary predicate for comparison. Syntax: forwardlist_name.unique(BinaryPredicate name)Parameters: The function accepts a single parameter which is a binary predica 2 min read forward_list::cend() in C++ STL with Exampleforward_list::cend() is a function in C++ STL which returns a constant iterator pointing to the past-the-last element of the forward_list. The iterator returned by the function does not point to any element in the container, but to the position followed by the last element of the forward list contai 2 min read forward_list emplace_after() and emplace_front() in C++ STLThe forward_list::emplace_after() is a builtin function in C++ STL which is used to insert a new element after the element at position specified in the argument. This insertion of the new element increases the size of the container by one. Syntax: forward_list_name.emplace_after(iterator position, e 2 min read forward_list resize() function in C++ STLThe forward_list::resize() is an inbuilt function in C++ STL which changes the size of forward_list. If the given size is greater than the current size then new elements are inserted at the end of the forward_list. If the given size is smaller than current size then extra elements are destroyed. Syn 2 min read forward_list::splice_after() in C++ STLforward_list::splice_after() is an inbuilt function in CPP STL which transfers the elements in the range of first+1 to last from a given forward_list to another forward_list. The elements are inserted after the element pointed to by position in the parameter. Syntax: forwardlist1_name.splice_after(p 2 min read forward_list cbegin() in C++ STLThe forward_list::cbegin() is a function in C++ STL which returns a constant iterator pointing to the first element of the forward_list. Syntax: forward_list_name.cbegin() Parameters: This function does not accept any parameter. Return Value: This function returns an iterator that points to the cons 2 min read forward_list::max_size() in C++ STLstd::forward_list::max_size() is an inbuilt function in CPP STL which returns the maximum number of elements can be held by forward_list. This value depends on system or library implementation. Syntax: forwardlist_name.max_size ()Parameters: The function does not accept any parameters. Return value: 1 min read forward_list::before_begin() in C++ STLforward_list::before_begin() is an inbuilt function in C++ STL that returns an iterator that points to the position before the first element of the forward_list. Forward list in STL is a singly linked list implementation. This function comes under the <forward_list> header file. Syntax: forwar 1 min read forward_list::cbefore_begin() in C++ STLforward_list::cbefore_begin() is an inbuilt function in CPP STL which returns a constant random access iterator which points to the position before the first element of the forward_list. The iterator obtained by this function can be used to iterate in the container but cannot be used to modify the c 2 min read forward_list merge() in C++ STLforward_list::merge() is an inbuilt function in C++ STL which merges two sorted forward_lists into one. The merge() function can be used in two ways: Merge two forward lists that are sorted in ascending order into one. Merge two forward lists into one using a comparison function. Syntax: forwardlist 2 min read Forward List and List of Pairs in C++ with Examples Forward List Forward list in STL implements singly linked list. Introduced from C++11, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differs from the list by the fact that the 8 min read Forward List and List of Tuples in C++ with Examples What is Forward List? Forward list in STL is used to implement a singly linked list. It was introduced from C++11 onwards, forward lists are more useful than other containers in insertion, removal, and moving operations (like sort) and allow time constant insertion and removal of elements. It differ 9 min read Difference Between Forward List and List in C++ Forward List is a sequence container that allows unidirectional sequential access to its data. It contains data of the same type. In STL, it has been implemented using Singly Linked List, which requires constant time for insertion and deletion. Elements of the forward list are scattered in the memor 3 min read Common Forward List ProgramsHow to find Size of std::forward_list in C++ STLForward list in standard template library of C++. It comes under #include<forward_list> header file. It is implemented as a singly linked list. It was introduced in C++ 11 for the first time. Forward lists are sequence containers that allow constant time insert and erase operations from anywhe 3 min read Like