multimap::count() in C++ STL

In this article we will be discussing the working, syntax, and examples of multimap::count() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container, there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::count()?

Multimap::count() function is an inbuilt function in C++ STL, which is defined in <map> header file. count() is used to count the number of elements are present with a specific key in a multimap associated with the function.

This function returns zero if the key is not present in the multimap container.


multimap_name.count(key_type& key);


The function accepts following parameter(s) −

  • key − This is the key which we want to search and count the number of elements associated with the key.

Return value

This function returns an integer i.e the number of elements with the same key.


std::multimap<char, int> odd, eve;
odd.insert(make_pair(‘a’, 1));
odd.insert(make_pair(‘a, 3));
odd.insert(make_pair(‘c’, 5));




 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 2, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 1, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   cout << "\nElements in multimap is : \n";
   cout <<"KEY\tELEMENT\n";
   for (auto i = mul.begin(); i!= mul.end(); i++){
      cout << i->first << "\t" << i->second << endl;
   cout<<"Key 1 appears " << mul.count(1) <<" times in the multimap\n";
   cout<<"Key 2 appears " << mul.count(2) <<" times in the multimap\n";
   return 0;


If we run the above code it will generate the following output −

Elements in multimap is :
1 50
1 40
1 10
2 30
2 20
5 60
Key 1 appears 3 times in the multimap
Key 2 appears 2 times in the multimap

Updated on: 22-Apr-2020


Kickstart Your Career

Get certified by completing the course

Get Started