The clear() Method is used to remove all the elements from the Set.
#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;
}
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,
-Method1.png)
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.