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




SET - EMPTY() METHOD


The empty() Method is used to check if a Set is empty or not.


Let us say, we have a Set that contains three names, Mohan, Kriti and Salim. And we want to check if the Set is empty or not.


We can use the front() Method to achieve the above.


Example :



#include <iostream>
#include <set>

using namespace std;

int main() {

    set<string> x = {"Mohan", "Kriti", "Salim"};
    if (x.empty()) {
    	cout << "The Set is empty"; 
    }	
    else {
    	cout << "The Set is not empty"; 
    } 
    
    return 0;    
}


Output :



  The Set is not empty

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 empty() method to check if the Set is empty or not.


if (x.empty())

In this case the Set is not empty.


if (x.empty()) {
	cout << "The Set is empty";
}
else {
	cout << "The Set is not empty";
}

So, we got the output as,

Output :



  The Set is not empty