unordered_set size() function in C++ STL

The unordered_set::size() method is a builtin function in C++ STL which is used to return the number of elements in the unordered_set container.

Syntax:

unordered_set_name.size()

Parameter: It does not accepts any parameter.



Return Value: The function returns the number of elements in the container.

Below programs illustrate the unordered_set::size() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the 
// unordered_set.size() function 
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<int> arr1 = { 1, 2, 3, 4, 5 };
  
    // prints the size of arr1
    cout << "size of arr1:" << arr1.size();
  
    // prints the element
    cout << "\nThe elements are: ";
    for (auto it = arr1.begin(); it != arr1.end(); it++)
        cout << *it << " ";
  
    return 0;
}

chevron_right


Output:

size of arr1:5
The elements are: 5 1 2 3 4

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_set::size() function
// when container is empty
#include <iostream>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<int> arr2 = {};
  
    // prints the size
    cout << "Size of arr2 : " << arr2.size();
  
    return 0;
}

chevron_right


Output:

Size of arr2 : 0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.