Skip to content
Related Articles

Related Articles

Improve Article
forward_list::clear() and forward_list::erase_after() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 31 May, 2021

Forward 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 location of only next element while list keeps track to both next and previous elements.
 

forward_list::clear()

clear() function is used to remove all the elements of the forward list container, thus making its size 0. 
Syntax : 
 

forwardlistname.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the forward list are
removed ( or destroyed )

Examples: 
 

Input  : flist{1, 2, 3, 4, 5};
         flist.clear();
Output : flist{}

Input  : flist{};
         flist.clear();
Output : flist{}

Errors and Exceptions
1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed.
 

CPP




// CPP program to illustrate
// Implementation of clear() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    forward_list<int> myflist{ 1, 2, 3, 4, 5 };
 
    myflist.clear();
    // Forward List becomes empty
 
    // Printing the Forward list
    for (auto it = myflist.begin(); it != myflist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 



No Output

 

forward_list::erase_after()

erase-after() function is used to remove elements from a container next to the specified position or from a range.
Syntax : 
 

1. flistname.erase_after(position)
2. flistname.erase_after(startingposition, endingposition)
Parameters :
Position previous of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the next
position of the container.

Examples: 
 

Input  : flist{1, 2, 3, 4, 5}, iterator= 2 i.e.,iterator is pointing to element with index 2.
         flist.erase_after(iterator);
Output : 1, 2, 3, 5

Input  : flist{1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6. i.e., iterator1 is pointing
         to element with index 3 and iterator2 is pointing to element with index 6.
         flist.erase_after(iterator1, iterator2);
Output : 1, 2, 3, 4, 7, 8

Errors and Exceptions
1. It has a no exception throw guarantee, if the position is valid. 
2. Shows undefined behaviour otherwise.
Removing element from particular position 
 

CPP




// CPP program to illustrate
// Implementation of erase_after() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    forward_list<int> myflist{ 1, 2, 3, 4, 5 };
    forward_list<int>::iterator it;
 
    it = myflist.begin();
    myflist.erase_after(it);
 
    // Printing the forward list
    for (auto it = myflist.begin(); it != myflist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

1 3 4 5

Removing elements within a range 
 

CPP




// CPP program to illustrate
// Implementation of erase_after() function
#include <forward_list>
#include <iostream>
using namespace std;
 
int main()
{
    forward_list<int> myflist{ 1, 2, 3, 4, 5 };
    forward_list<int>::iterator it1, it2;
 
    it1 = myflist.begin();
    it2 = myflist.end();
 
    myflist.erase_after(it1, it2);
 
    // Printing the forward list
    for (auto it = myflist.begin(); it != myflist.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 
 

1

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :