erase_if() Function in C++
The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and associative containers.
In this article, we will learn about the erase_if() function, its syntax, usage scenarios, and practical examples to demonstrate how it can be used effectively in C++ programming.
Syntax of erase_if() in C++
erase_if(container, predicate);
Parameters of std::erase_if()
- container is the container from which elements will be removed.
- predicate is a unary function that determines whether an element should be erased as it return a true if the element should be removed and false otherwise.
Return Value of std::erase_if()
The std::erase_if() function returns the number of elements removed from the container.
Supported Containers
The erase_if() function is supported for the following standard containers:
- Sequence Containers: std::vector, std::deque, std::list, std::forward_list
- Associative Containers: std::map, std::set, std::multimap, std::multiset
- Unordered Containers: std::unordered_map, std::unordered_set, std::unordered_multimap, std::unordered_multiset
Examples of std::erase_if() in C++
The below examples demonstrates how we can use the erase_if() function to remove elements from a container based on a condition.
Example 1: Removing Negative Numbers from a Vector
//C++ proggram to use std::erase_if() for Removing Negative Numbers from a Vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// Initialize a vector with a mix of positive and negative integers
vector<int> numbers = {1, -2, 3, -4, 5, -6, 7};
// Use erase_if to remove all negative numbers from the vector
auto count = erase_if(numbers, [](int x) { return x < 0; });
cout << "Number of elements removed: " << count << endl;
// print the remaining elements in the vector after removal
cout << "Remaining elements: ";
for (int n : numbers) {
cout << n << " ";
}
cout << endl;
return 0;
}
Output
Number of elements removed: 3
Remaining elements: 1 3 5 7
Time Complexity: O(n), where n is the number of elements in the vector.
Auxiliary Space: O(1)
Example 2: Removing Entries from a Map Based on Value
//C++ program to
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
// Initialize a map with integer keys and values
map<int, int> data = {{1, 100}, {2, 200}, {3, 150}, {4, 100}, {5, 250}};
// Use erase_if to remove all entries with the value 100 from the map
auto count = erase_if(data, [](const pair<int, int> &p) { return p.second == 100; });
// print the number of elements removed from the map
cout << "Number of elements removed: " << count << endl;
// printthe remaining elements in the map after removal
cout << "Remaining elements: ";
for (const auto &[key, value] : data){
// Print each key-value pair in the map
cout << "{" << key << ", " << value << "} ";
}
cout << endl;
return 0;
}
Output
Number of elements removed: 2
Remaining elements: {2, 200} {3, 150} {5, 250}
Time Complexity: The time complexity of the std::erase_if() function depends on the container type:
- For std::vector and std::deque, it is O(n).
- For std::list and std::forward_list, it is O(n) with bidirectional iterators.
- For associative containers like std::set, std::map, std::unordered_set, and std::unordered_map, it depends on the underlying implementation but generally is O(n) for linear search.
Auxiliary Space: The auxiliary space required by std::erase_if() is O(1) as it operates in-place within the container.