list remove() function in C++ STL
The list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. It removes elements comparing to a value. It takes a value as the parameter and removes all the elements from the list container whose value is equal to the value passed in the parameter of the function.
Syntax:
list_name.remove(val)
Parameters: This function accepts a single parameter val which refers to the value of elements needed to be removed from the list. The remove() function will remove all the elements from the list whose value is equal to val.
Return Value: This function does not returns any value.
Below program illustrates the list::remove() function.
// CPP program to illustrate the // list::remove() function #include <bits/stdc++.h> using namespace std; int main() { // Creating a list list<int> demoList; // Add elements to the List demoList.push_back(10); demoList.push_back(20); demoList.push_back(20); demoList.push_back(30); demoList.push_back(40); // List before removing elements cout << "List before removing elements: "; for (auto itr = demoList.begin(); itr != demoList.end(); itr++) { cout << *itr << " "; } // delete all elements with value 20 demoList.remove(20); // List after removing elements cout << "\nList after removing elements: "; for (auto itr = demoList.begin(); itr != demoList.end(); itr++) { cout << *itr << " "; } return 0; } |
List before removing elements: 10 20 20 30 40 List after removing elements: 10 30 40
Note: This function works in linear time complexity.
Recommended Posts:
- list::remove() and list::remove_if() in C++ STL
- list end() function in C++ STL
- list merge() function in C++ STL
- list max_size() function in C++ STL
- list emplace() function in C++ STL
- list splice() function in C++ STL
- list pop_back() function in C++ STL
- list pop_front() function in C++ STL
- list push_front() function in C++ STL
- list reverse function in C++ STL
- list resize() function in C++ STL
- list size() function in C++ STL
- list push_back() function in C++ STL
- list assign() function in C++ STL
- list back() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



