How to Remove Items from Set based on a Condition in C++


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,

  1. We include the necessary headers: <iostream>, <set>, and <algorithm> for the necessary functionalities.
  2. We create a std::set<int> named number_set and initialize it with the values {1, 2, 3, 4, 5}.
  3. 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.
  4. 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, since std::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 the erase method directly.
  5. We iterate over the set and remove elements that satisfy the condition defined by the lambda function using the erase method.
  6. Finally, we print the modified set to the console to see the remaining items.

C++ Program

#include &lt;iostream&gt;
#include &lt;set&gt;
#include &lt;algorithm&gt;

int main() {
    std::set&lt;int&gt; 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 &lt;&lt; num &lt;&lt; " ";
    }
    std::cout &lt;&lt; std::endl;
    return 0;
}

Output

1 3 5 

2 Removing Short Strings from Character Set

In this example,

  1. We include the necessary headers: <iostream>, <set>, and <string> for the necessary functionalities.
  2. We create a std::set<std::string> named string_set and initialize it with the values {"apple", "banana", "cherry", "date", "grape"}.
  3. 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.
  4. We iterate over the set and remove elements that satisfy the condition defined by the lambda function using the erase method.
  5. Finally, we print the modified set to the console to see the remaining items.

C++ Program

#include &lt;iostream&gt;
#include &lt;set&gt;
#include &lt;string&gt;

int main() {
    std::set&lt;std::string&gt; string_set = {"apple", "banana", "cherry", "date", "grape"};
    for(auto it = string_set.begin(); it != string_set.end(); ) {
        if (it->length() &lt; 6) {
            it = string_set.erase(it);
        } else {
            ++it;
        }
    }
    for(const std::string &str : string_set) {
        std::cout &lt;&lt; str &lt;&lt; " ";
    }
    std::cout &lt;&lt; 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

  1. How to create an Empty Set in C++ ?
  2. How to Initialize a Set in C++ ?
  3. How to Get Length of a Set in C++ ?
  4. How to create a Set of size N in C++ ?
  5. How to create a Set of Numbers from 1 to N in C++ ?
  6. How to create a Set of integers in C++ ?
  7. How to create a Set of Strings in C++ ?
  8. How to Access Items in a Set in C++ ?
  9. How to get a Random Item in a Set in C++ ?
  10. How to Iterate Over a Set in C++ ?
  11. How to check if a Set is Empty in C++ ?
  12. How to check if a Set is Not Empty in C++ ?
  13. How to get Subset from a Set in C++ ?
  14. How to check if a Specific Item is present in the Set in C++ ?
  15. How to check if a Set contains all the items of Another Set in C++ ?
  16. How to Sort Items of a Set in C++ ?
  17. How to Copy a Set in C++ ?
  18. How to add an Item to a Set in C++ ?
  19. How to find Union of Two Sets in C++ ?
  20. How to find Intersection of Two Sets in C++ ?
  21. How to check if Two Sets are Equal in C++ ?
  22. How to Convert a Set of Integers to a Set of Strings in C++ ?
  23. How to Convert a Set of Strings to a Set of Integers in C++ ?
  24. How to Convert a Set of Floats to a Set of Strings in C++ ?
  25. How to Filter Items of a Set based on a Condition in C++ ?
  26. How to Remove Specific Item from a Set in C++ ?
  27. How to Remove Items from Set based on a Condition in C++ ?
  28. How to create a Set of Sets in C++ ?