C++ List::remove() Function



The C++ std::list::remove() function is used to remove the element from the list. It accepts a parameter as any(int, char, string, etc.) type and removes the specified element from the current list of matches; otherwise, no changes reflect in the current list. It reduces the size of the list by the number of elements removed from the list. It does not return any value because the function return type is void.

Syntax

Following is the syntax of the C++ std::list::remove() function −

void remove (const value_type& val);

Parameters

  • val − It is a value of the element to be removed.

Return Value

This function does not return any value.

Example 1

If the given element is present in the list.

In the following program, we are using the C++ std::list::remove() function to remove the specified element 20 from the current list {10, 20, 30, 40, 50, 60} if matched(or present).

#include<iostream>
#include<list>
using namespace std;

int main(void) {
   list<int> lst = {10, 20, 30, 40, 50, 60};
   cout<<"The list elements before the remove operation: "<<endl;
   for(int l : lst){
      cout<<l<<" ";
   }
   int ele = 10;
   cout<<"\nThe element value: "<<ele;
   //using the remove() function
   lst.remove(10);
   cout<<"\nThe list elements after the remove operation: ";
   for(int l1 : lst){
      cout<<l1<<" ";
   }
}

Output

on executing the above program, it will produce the following output −

The list elements before the remove operation: 
10 20 30 40 50 60 
The element value: 10
The list elements after the remove operation: 20 30 40 50 60 

Example 2

If the given element is not present in the list, the remove() function does not remove any element from the list, and no changes reflect in the current list.

The following is another example of the std::list::remove() function, here, we are creating a list(type char) with the values {'A', 'B', 'C', 'D'}, and using this function, we are trying to remove the specified element 'E' from this list if matched.

#include<iostream>
#include<list>
using namespace std;

int main(void) {
   list<char> lst = {'A', 'B', 'C', 'D'};
   cout<<"The list elements before the remove operation: "<<endl;
   for(char l : lst){
      cout<<l<<" ";
   }
   char ele = 'E';
   cout<<"\nThe element value: "<<ele;
   //using the remove() function
   lst.remove(ele);
   cout<<"\nThe list elements after the remove operation: ";
   for(char l1 : lst){
      cout<<l1<<" ";
   }
}

Output

Following is the output of the above program −

The list elements before the remove operation: 
A B C D 
The element value: E
The list elements after the remove operation: A B C D 

Example 3

You can also removes a string value from the list(type string).

In this example, we are creating a list(type string) named fruits with the values {"Mango", "Banana", "Apple", "Grapes"}. Then, using the std::list::remove() function, we are trying to remove the element 'Apple' from this list if present.

#include<iostream>
#include<list>
using namespace std;

int main(void) {
   list<string> fruits = {"Mango", "Banana", "Apple", "Grapes"};
   cout<<"The list elements before the remove operation: "<<endl;
   for(string l : fruits){
      cout<<l<<" ";
   }
   string ele = "Apple";
   cout<<"\nThe element value: "<<ele;
   //using the remove() function
   fruits.remove(ele);
   cout<<"\nThe list elements after the remove operation: ";
   for(string l1 : fruits){
      cout<<l1<<" ";
   }
}

Output

The above program generates the following output −

The list elements before the remove operation: 
Mango Banana Apple Grapes 
The element value: Apple
The list elements after the remove operation: Mango Banana Grapes 
Advertisements