C++ Standard Library C++ STL Library

C++ unordered_map - size() Function



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

Syntax

size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the unordered_map.

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<int, string> uMap;

  uMap[101] = "John";
  uMap[102] = "Marry";
  uMap[103] = "Kim";
  uMap[104] = "Jo";
  uMap[105] = "Ramesh";

  cout<<"uMap size is: "<<uMap.size()<<"\n";

  cout<<"Three key/element pairs are added in uMap.\n";
  uMap[106] = "Suresh";
  uMap[107] = "Jack";
  uMap[108] = "Adam";

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

The output of the above code will be:

uMap size is: 5
Three key/element pairs are added in uMap.
Now, uMap size is: 8

❮ C++ <unordered_map> Library