C++ std::list::remove_if
The std::list::remove_if
function removes all elements from a std::list
for which a specified predicate returns true
. This allows you to filter elements from the list based on a custom condition. The function is particularly useful for removing elements that satisfy complex criteria.
Syntax of std::list::remove_if
template <class Predicate>
void remove_if(Predicate pred);
Parameters
Parameter | Description |
---|---|
pred | A unary predicate function that returns true for elements that should be removed. The predicate should accept an element of the list as an argument. |
Return Value
This function does not return a value. It modifies the list in place by removing elements that satisfy the predicate.
Exceptions
The std::list::remove_if
function does not throw exceptions unless the predicate itself throws an exception. It provides strong exception safety: if an exception is thrown, the list remains unchanged.
Examples for std::list::remove_if
Example 1: Removing Odd Numbers
This example demonstrates how to use remove_if
to remove all odd numbers from a list:
Program
#include <iostream>
#include <list>
bool isOdd(int value) {
return value % 2 != 0;
}
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
std::cout << "List contents before remove_if: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Remove all odd numbers
myList.remove_if(isOdd);
std::cout << "List contents after remove_if: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{1, 2, 3, 4, 5}
. - A predicate function
isOdd
is defined to check if a number is odd. - The
remove_if
function is called withisOdd
as the predicate, removing all odd numbers from the list. - The modified list contents are printed, showing that only the even numbers remain.
Output:
List contents before remove_if: 1 2 3 4 5
List contents after remove_if: 2 4
Example 2: Removing Strings with a Specific Condition
This example demonstrates how to use remove_if
to remove strings with less than 5 characters:
Program
#include <iostream>
#include <list>
#include <string>
bool isShort(const std::string& str) {
return str.size() < 5;
}
int main() {
std::list<std::string> myList = {"apple", "dog", "banana", "cat"};
std::cout << "List contents before remove_if: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Remove strings with less than 5 characters
myList.remove_if(isShort);
std::cout << "List contents after remove_if: ";
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", "dog", "banana", "cat"}
. - A predicate function
isShort
is defined to check if the length of a string is less than 5 characters. - The
remove_if
function is called withisShort
as the predicate, removing all short strings from the list. - The modified list contents are printed, showing only the strings with 5 or more characters.
Output:
List contents before remove_if: apple dog banana cat
List contents after remove_if: apple banana
Example 3: Removing Elements Using a Lambda Expression
This example demonstrates how to use a lambda expression with remove_if
to remove elements greater than a specified value:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 10, 20, 30, 5};
std::cout << "List contents before remove_if: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Remove elements greater than 15 using a lambda
myList.remove_if([](int value) { return value > 15; });
std::cout << "List contents after remove_if: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with{1, 10, 20, 30, 5}
. - A lambda expression is used as the predicate to check if an element is greater than 15.
- The
remove_if
function is called with the lambda expression, removing all elements greater than 15. - The modified list contents are printed, showing only elements less than or equal to 15.
Output:
List contents before remove_if: 1 10 20 30 5
List contents after remove_if: 1 10 5