The size() Method is used to give us the number of elements in the Set.
Let us say, we have a Set that contains three names, Mohan, Kriti and Salim. And we want to check how many elements are there in the Set.
We can use the size() Method to achieve the above.
#include <iostream> #include <set> using namespace std; int main() { set<string> x = {"Mohan", "Kriti", "Salim"}; int count = x.size(); cout << "The number of elements in the Set are : " << count; 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 size() method to get the number of elements in the Set and store it in a variable count.
int count = x.size();
And when we print the value of count variable, we get 3 as output.