How to Remove Items from Set based on a Condition in C++ - Step by Step Examples
How to Remove Items from Set based on a Condition in C++ ?
Answer
To remove items from a set based on a condition in C++, you can use the std::remove_if
function along with a lambda expression and then erase the unwanted elements from the set.
✐ Examples
1 Removing Even Numbers from Integer Set
In this example,
- We include the necessary headers:
<iostream>
,<set>
, and<algorithm>
for the necessary functionalities. - We create a
std::set<int>
namednumber_set
and initialize it with the values{1, 2, 3, 4, 5}
. - We use a lambda function to define the condition for removing elements. The lambda function
[](int x) { return x % 2 == 0; }
checks if a number is even. - We use the
std::remove_if
algorithm to rearrange the elements in the set such that the elements to be removed are moved to the end. However, sincestd::remove_if
does not work directly with sets due to their nature of having unique and ordered elements, we need to use a loop and theerase
method directly. - We iterate over the set and remove elements that satisfy the condition defined by the lambda function using the
erase
method. - Finally, we print the modified set to the console to see the remaining items.
C++ Program
#include <iostream>
#include <set>
#include <algorithm>
int main() {
std::set<int> number_set = {1, 2, 3, 4, 5};
for(auto it = number_set.begin(); it != number_set.end(); ) {
if (*it % 2 == 0) {
it = number_set.erase(it);
} else {
++it;
}
}
for(const int &num : number_set) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
Output
1 3 5
2 Removing Short Strings from Character Set
In this example,
- We include the necessary headers:
<iostream>
,<set>
, and<string>
for the necessary functionalities. - We create a
std::set<std::string>
namedstring_set
and initialize it with the values{"apple", "banana", "cherry", "date", "grape"}
. - We use a lambda function to define the condition for removing elements. The lambda function
[](const std::string& s) { return s.length() < 6; }
checks if a string's length is less than 6. - We iterate over the set and remove elements that satisfy the condition defined by the lambda function using the
erase
method. - Finally, we print the modified set to the console to see the remaining items.
C++ Program
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> string_set = {"apple", "banana", "cherry", "date", "grape"};
for(auto it = string_set.begin(); it != string_set.end(); ) {
if (it->length() < 6) {
it = string_set.erase(it);
} else {
++it;
}
}
for(const std::string &str : string_set) {
std::cout << str << " ";
}
std::cout << std::endl;
return 0;
}
Output
banana cherry
Summary
In this tutorial, we learned How to Remove Items from Set based on a Condition in C++ language with well detailed examples.
More C++ Sets Tutorials
- How to create an Empty Set in C++ ?
- How to Initialize a Set in C++ ?
- How to Get Length of a Set in C++ ?
- How to create a Set of size N in C++ ?
- How to create a Set of Numbers from 1 to N in C++ ?
- How to create a Set of integers in C++ ?
- How to create a Set of Strings in C++ ?
- How to Access Items in a Set in C++ ?
- How to get a Random Item in a Set in C++ ?
- How to Iterate Over a Set in C++ ?
- How to check if a Set is Empty in C++ ?
- How to check if a Set is Not Empty in C++ ?
- How to get Subset from a Set in C++ ?
- How to check if a Specific Item is present in the Set in C++ ?
- How to check if a Set contains all the items of Another Set in C++ ?
- How to Sort Items of a Set in C++ ?
- How to Copy a Set in C++ ?
- How to add an Item to a Set in C++ ?
- How to find Union of Two Sets in C++ ?
- How to find Intersection of Two Sets in C++ ?
- How to check if Two Sets are Equal in C++ ?
- How to Convert a Set of Integers to a Set of Strings in C++ ?
- How to Convert a Set of Strings to a Set of Integers in C++ ?
- How to Convert a Set of Floats to a Set of Strings in C++ ?
- How to Filter Items of a Set based on a Condition in C++ ?
- How to Remove Specific Item from a Set in C++ ?
- How to Remove Items from Set based on a Condition in C++ ?
- How to create a Set of Sets in C++ ?