forward_list::remove() in C++ STL


In this article we will be discussing the working, syntax and examples of forward_list::remove() and forward_list::remove_if() functions in C++.

What is a Forward_list in STL?

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.

What is forward_list::remove()?

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.

Syntax

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.

Return Value

This function returns nothing

Example

In the below code we are

 Live Demo

#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;
}

Output

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

Updated on: 02-Mar-2020

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements