Skip to content
Related Articles

Related Articles

forward_list::clear() and forward_list::erase_after() in C++ STL

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2022
View Discussion
Improve Article
Save Article

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

 Time Complexity: O(N)

Auxiliary Space: O(1)
 

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

 Time Complexity: O(N)
Auxiliary Space: O(1) 

Let us see the differences in a tabular form -:

  forward_list::clear()  forward_list::erase_after() 
1. It is used to remove all elements from the forward_list container It is used to remove from the forward_list container either a single element or a range of elements.
2.

Its syntax is -:

clear();

Its syntax is -:

iterator erase_after (const_iterator position);

3. It does not take any parameters.

It only takes two parameters that are -:

1. Iterator pointing to an element in the forward_list container. 

2. Iterator pointing to the element after the last one to be removed.

4. It does not have any return value. Its complexity is Linear.
5. Its complexity is Linear. It is present in <forward_list> header file.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!