Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


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 below example, 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<<"Unordered Map size is: "<<uMap.size()<<"\n\n";

  cout<<"Add three key/element pairs in the Unordered Map.\n";
  uMap[106] = "Suresh";
  uMap[107] = "Jack";
  uMap[108] = "Adam";

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

The output of the above code will be:

Unordered Map size is: 5

Add three key/element pairs in the Unordered Map.
Now, Unordered Map size is: 8

❮ C++ - Unordered Map