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.
#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; }
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,
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,