C++ Standard Library C++ STL Library

C++ multimap - size() Function



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

Note: Multimap is an ordered data container which implies all its elements are ordered all the time.

Syntax

size_type size() const;
size_type size() const noexcept;

Parameters

No parameter is required.

Return Value

Number of elements present in the multimap.

Time Complexity

Constant i.e, Θ(1).

Example:

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MyMMap;

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

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

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

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

The output of the above code will be:

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

❮ C++ <map> Library