Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




SET - ERASE() METHOD


erase() method remove an element from the Set by its name/value.


Let us say, we have a Set that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the Set.


It can be done with the erase() Function


Example :



#include <iostream>
#include <set>

using namespace std;

int main() {

    set<string> x = {"Mohan", "Kriti", "Salim"};
    x.erase("Kriti");

    for (string data : x) {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :



  Mohan
  Salim

So, in the above code we have created a Set and initialised to the variable x.


set<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the Set,

java_Collections

Next, we have used the remove() function that searches for the name Kriti and removes it from the Set.


x.remove("Kriti");
java_Collections


And we get the below output,

Output :



  Mohan
  Salim