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


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

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
         flist.erase_after(iterator);
Output : 1, 2, 3, 5

Input  : flist{1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
         flist.erase(iterator1, iterator2);
Output : 1, 2, 3, 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output:

1 3 4 5

Removing elements within a range

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output:

1

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




Article Tags :
C++
Practice Tags :