C++ Standard Library C++ STL Library

C++ unordered_set - size() Function



The C++ unordered_set::size function is used to find out the total number of elements in the unordered_set.

Syntax

size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the unordered_set.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the unordered_set::size function is used find out the total number of elements in a unordered_set called uSet.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_set<int> uSet{55, 25, 128, 5, 72};

  cout<<"Unordered Set size is: "<<uSet.size()<<"\n";

  cout<<"Three elements are added in the Unordered Set.\n";
  uSet.insert(9);
  uSet.insert(22);
  uSet.insert(54);

  cout<<"Now, Unordered Set size is: "<<uSet.size()<<"\n";
  return 0;
}

The output of the above code will be:

Unordered Set size is: 5
Three elements are added in the Unordered Set.
Now, Unordered Set size is: 8

❮ C++ <unordered_set> Library