C++ Get the Size of the Unordered Set
ht//:sptwww.theitroad.com
To get the number of elements in an std::unordered_set
container in C++, you can use the size()
method. The size()
method returns the number of elements in the set. Here's an example:
#include <iostream> #include <unordered_set> int main() { std::unordered_set<int> mySet = {1, 2, 3, 4, 5}; // Get the size of the set std::cout << "Size of set: " << mySet.size() << std::endl; return 0; }
This program creates an std::unordered_set
container called mySet
with the values 1, 2, 3, 4, and 5. It then demonstrates how to get the size of the set using the size()
method. Finally, it prints the size of the set to the console. The output of this program will be:
Size of set: 5
Note that the size()
method returns the number of elements in the set, which in this case is 5.