remove() method remove an element from the List by its name/value.
Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the List.
It can be done with the remove() Function
#include <iostream> #include <list> using namespace std; int main() { list<string> x = {"Mohan", "Kriti", "Salim"}; x.remove("Kriti"); for (string data : x) { cout << data << endl; } return 0; }
So, in the above code we have created a List and initialised to the variable x.
list<string> x = {"Mohan", "Kriti", "Salim"};
Below is how the values are positioned in the List,
Next, we have used the remove() function that searches for the name Kriti and removes it from the List.
x.remove("Kriti");
And we get the below output,