C++ std::list::remove
The std::list::remove
function removes all elements in a std::list
that are equal to a specified value. This function is particularly useful for filtering out elements from the list. It performs the operation in linear time, traversing the list and removing all matching elements.
Syntax of std::list::remove
void remove(const value_type& val);
Parameters
Parameter | Description |
---|---|
val | The value of the elements to be removed from the list. All elements equal to this value will be removed. |
Return Value
This function does not return a value. It modifies the list by removing all matching elements.
Exceptions
The std::list::remove
function does not throw exceptions. It provides a no-throw guarantee since it only involves traversing the list and modifying internal pointers.
Examples for std::list::remove
Example 1: Removing a Single Value
This example demonstrates how to use remove
to eliminate all occurrences of a specific value from the list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30, 20, 40};
std::cout << "List contents before remove: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Remove all occurrences of the value 20
myList.remove(20);
std::cout << "List contents after remove: ";
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, 20, 40}
. - The
remove
function is called with the value20
. All occurrences of this value are removed from the list. - The modified contents of
myList
are printed, showing that the value20
has been removed.
Output:
List contents before remove: 10 20 30 20 40
List contents after remove: 10 30 40
Example 2: Removing Elements from an Empty List
This example demonstrates that calling remove
on an empty list does nothing:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
// Try to remove a value from an empty list
myList.remove(10);
std::cout << "List size after remove on an empty list: " << myList.size() << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is created and is initially empty. - The
remove
function is called with the value10
, but since the list is empty, nothing happens. - The
size()
function is used to confirm that the list remains empty after the operation.
Output:
List size after remove on an empty list: 0
Example 3: Removing Elements from a List of Strings
This example demonstrates how remove
works with a std::list
containing strings:
Program
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> myList = {"apple", "banana", "apple", "cherry"};
std::cout << "List contents before remove: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Remove all occurrences of "apple"
myList.remove("apple");
std::cout << "List contents after remove: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
of strings namedmyList
is initialized with{"apple", "banana", "apple", "cherry"}
. - The
remove
function is called with the value"apple"
. All occurrences of this value are removed from the list. - The modified contents of
myList
are printed, showing that"apple"
has been removed.
Output:
List contents before remove: apple banana apple cherry
List contents after remove: banana cherry