In this article we will be discussing the working, syntax and examples of forward_list::remove() and forward_list::remove_if() functions in C++.
Forward list are sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward list are implement as a singly-linked lists. The ordering is kept by the association to each element of a link to the next element in the sequence.
forward_list::remove() is an inbuilt function in C++ STL which is declared in header file. remove() is used to removes all the elements from the forward_list. The container size is decresed by the number of elements removed.
flist_container1.remove(const value_type& value );
This function can accept only one parameter, i.e. the value which is to be inserted at the beginning.
This function returns nothing
In the below code we are
#include <forward_list> #include <iostream> using namespace std; int main(){ forward_list<int> forwardList = {2, 3, 1, 1, 1, 6, 7}; //List before applying remove operation cout<<"list before applying remove operation : "; for(auto i = forwardList.begin(); i != forwardList.end(); ++i) cout << ' ' << *i; //List after applying remove operation cout<<"\nlist after applying remove operation : "; forwardList.remove(1); for(auto i = forwardList.begin(); i != forwardList.end(); ++i) cout << ' ' << *i; }
If we run the above code it will generate the following output
list before applying remove operation : 2, 3, 1, 1, 1, 6, 7 list after applying remove operation : 2, 3, 6, 7