To remove a specific element from a list in C++ using `std::list`, you can use the `std::list::remove` method.
In this example,
myList
with some elements.remove
method of the list to remove the element if it is present.#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
int valueToRemove = 3;
myList.remove(valueToRemove);
for (auto& element : myList) {
std::cout << element << " ";
}
return 0;
}
1 2 4 5
In this example,
myList
with some elements.remove
method of the list to attempt to remove the element.#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
int valueToRemove = 6;
myList.remove(valueToRemove);
for (auto& element : myList) {
std::cout << element << " ";
}
return 0;
}
1 2 3 4 5
In this tutorial, we learned How to Remove Specific Element from a List in C++ language with well detailed examples.