The list::erase() is a built-in function in C++ STL which is used to delete elements from a list container. This function can be used to remove a single element or a range of elements from the specified list container.
iterator list_name.erase(iterator position) or, iterator list_name.erase(iterator first, iterator last)
Parameters: This function can accepts different parameters based on whether it is used to erase a single element or a range of element from the list container.
- position: This parameter is used when the function is used to delete a single element. This parameter refers to an iterator which points to the element which is need to be erased from the list container.
- first, last: These two parameters are used when the list is used to erase elements from a range. The parameter first refers to the iterator pointing to the first element in the range and the parameter last refers to the iterator pointing to the last element in the range which is needed to be erased. This erases all the elements in the range including the element pointed by the iterator first but excluding the element pointed by the iterator last.
Return Value: This function returns an iterator pointing to the element in the list container which followed the last element erased from the list container.
Below programs illustrates the list::erase() function.
Program 1: Erasing a single element.
List before deleting first element: 10 20 30 40 50 List after deleting first element:20 30 40 50
Program 2: Erasing a range of elements.
List before deleting any element: 10 20 30 40 50 List after deleting first three elements: 40 50
Note: This function works in linear time complexity, that is the number of elements erased from the list container.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- How to erase an element from a vector using erase() and reverse_iterator?
- map erase() function in C++ STL
- unordered_multiset erase() function in C++ STL
- unordered_set erase() function in C++ STL
- set::erase in C++ STL
- unordered_map erase in C++ STL
- unordered_multimap erase in C++ STL
- std::string::erase in C++
- multimap::erase() in C++ STL
- multiset erase() in C++ STL
- vector erase() and clear() in C++
- Different ways to delete elements in std::map (erase() and clear())
- Difference between std::remove and vector::erase for vectors
- deque::clear() and deque::erase() in C++ STL
- list end() function in C++ STL
- list pop_back() function in C++ STL
- list assign() function in C++ STL
- list front() function in C++ STL
- list remove() function in C++ STL
- list resize() 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 email@example.com. 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.