C++ Standard Library C++ STL Library

C++ unordered_multimap - size() Function



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

Syntax

size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the unordered_multimap.

Time Complexity

Constant i.e, Θ(1).

Example:

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

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

  uMMap.insert(pair<string, string>("USA", "New York"));
  uMMap.insert(pair<string, string>("USA", "Washington"));  
  uMMap.insert(pair<string, string>("CAN", "Toronto"));
  uMMap.insert(pair<string, string>("CAN", "Montreal"));
  uMMap.insert(pair<string, string>("IND", "Delhi"));

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

  cout<<"Three key/element pairs are added in uMMap.\n";
  uMMap.insert(pair<string, string>("UK", "London"));
  uMMap.insert(pair<string, string>("IND", "Mumbai"));
  uMMap.insert(pair<string, string>("USA", "Florida"));

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

The output of the above code will be:

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

❮ C++ <unordered_map> Library