C++ - Forward List remove() Function
The C++ forward_list::remove function is used to delete all occurrences of specified element from the forward_list container.
Syntax
void remove (const value_type& val);
Parameters
val |
Specify the value of the element which need to be removed from the forward_list. |
Return Value
None.
Time Complexity
Linear i.e, Θ(n).
Example:
In the below example, the forward_list::remove function is used to delete all occurrences of specified element from the list called f_list.
#include <iostream> #include <forward_list> using namespace std; int main (){ forward_list<int> f_list{10, 20, 30, 40, 50, 30, 30, 40}; forward_list<int>::iterator it; cout<<"f_list contains: "; for(it = f_list.begin(); it != f_list.end(); it++) cout<<*it<<" "; //Remove all occurrences of 30 from f_list f_list.remove(30); cout<<"\nf_list contains: "; for(it = f_list.begin(); it != f_list.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
f_list contains: 10 20 30 40 50 30 30 40 f_list contains: 10 20 40 50 40
❮ C++ - Forward List