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




SET - CLEAR() METHOD


The clear() Method is used to remove all the elements from the Set.


Example :



#include <iostream>
#include <set>

using namespace std;

int main() {

    set<string> x = {"Mohan", "Kriti", "Salim"};
        
    x.clear();
    for (string data : x)  
    {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :




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 clear() method that removes all the elements from the Set making the Set empty.


x.clear();

And we get an empty Set as output.