erase_if() Function in C++
Last Updated :
22 Aug, 2024
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++
//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++
//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.
Similar Reads
cin.clear() function in C++ The cin.clear() function in C++ is a member function of the std::istream class, which is used to clear the error flags on the cin stream. When an input operation fails (for example, due to incorrect data type input), the cin stream enters a fail state, and further input operations are blocked until
4 min read
cin.ignore() Function in C++ The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input o
3 min read
multiset::erase() in C++ STL The std::multiset::erase() is a built-in STL function used to remove elements from the multiset container. It is member function of std::multiset class defined inside <multiset> header file. In this article, we will learn about std::multiset::erase() in C++.The multiset::erase() function provi
3 min read
count_if() in C++ STL count_if() function returns the number of elements in a range that satisfy the condition. Syntax: template <class InputT, class UnaryPredicate> typename iterator_traits <InputT> :: difference_type count_if(InputT first, InputT last, UnaryPredicate p); Examples: Input: 0 1 2 3 4 5 6 7 8 9
2 min read
How to Use the ignore() Function in C++? In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio
2 min read