To get the size of a set in C++, you can use the size
method on the set.
In this example,
<set>
for using sets in C++.set1
using the std::set
template with some elements.size
method on the set object set1
to get the number of elements in the set.size
method returns the size of the set.#include <iostream>
#include <set>
int main() {
std::set<std::string> set1 {"apple", "banana", "cherry"};
size_t size = set1.size();
std::cout << "Size of set1: " << size << std::endl;
return 0;
}
Size of set1: 3
In this example,
<set>
for using sets in C++.emptySet
using the std::set
template.size
method on the set object emptySet
to get the number of elements in the set.size
method returns 0.#include <iostream>
#include <set>
int main() {
std::set<std::string> emptySet;
size_t size = emptySet.size();
std::cout << "Size of emptySet: " << size << std::endl;
return 0;
}
Size of emptySet: 0
In this tutorial, we learned How to Get Length of a Set in C++ language with well detailed examples.