C++ std::list::erase
The std::list::erase
function removes elements from a std::list
at the specified position or within a specified range. It reduces the size of the list and ensures that iterators to the erased elements are invalidated. The function is useful for removing single elements or a block of elements from the list.
Syntax of std::list::erase
// 1. Erase a single element
iterator erase(const_iterator position);
// 2. Erase a range of elements
iterator erase(const_iterator first, const_iterator last);
Parameters
Parameter | Description |
---|---|
position | A constant iterator specifying the position of the element to erase. |
first , last | Iterators specifying the range of elements to erase. The range includes all elements from first to last (but not last ). |
Return Value
The function returns an iterator pointing to the element that follows the last erased element. If the last erased element is the last element of the list, the returned iterator is equal to end()
.
Exceptions
The std::list::erase
function does not throw exceptions itself. However, invalid iterators or undefined ranges may lead to undefined behavior. Always ensure that the iterators are valid and within the list’s range.
Examples for std::list::erase
Example 1: Erasing a Single Element
This example demonstrates how to use erase
to remove a single element from a list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30, 40};
// Erase the second element
myList.erase(++myList.begin());
std::cout << "List contents after erase: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{10, 20, 30, 40}
. - The
erase
function is called with++myList.begin()
, which points to the second element (20
). - The element
20
is removed, and the list now contains{10, 30, 40}
. - A range-based
for
loop iterates through the list and prints its contents.
Output:
List contents after erase: 10 30 40
Example 2: Erasing a Range of Elements
This example demonstrates how to use erase
to remove a range of elements from a list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30, 40, 50};
// Erase elements from the second to the fourth
myList.erase(++myList.begin(), --myList.end());
std::cout << "List contents after erase: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{10, 20, 30, 40, 50}
. - The
erase
function is called with a range defined by++myList.begin()
(pointing to20
) and--myList.end()
(pointing to40
). - The elements
20
,30
, and40
are removed, leaving{10, 50}
. - A range-based
for
loop iterates through the list and prints its contents.
Output:
List contents after erase: 10 50
Example 3: Handling Erase with an Empty List
This example demonstrates how to safely use erase
with an empty list to avoid undefined behavior:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
if (!myList.empty()) {
myList.erase(myList.begin());
} else {
std::cout << "List is empty, cannot erase elements." << std::endl;
}
return 0;
}
Explanation:
- A
std::list
namedmyList
is created and is initially empty. - The
empty()
method is used to check if the list contains elements before attempting to callerase
. - If the list is empty, a message is printed to indicate that the operation cannot be performed.
Output:
List is empty, cannot erase elements.