Multimaps are associative containers available in the C++ Standard Library. They are similar to a map with an addition that multiple elements can have the same keys. The multimap::count()
is a method available in the STL that counts the number of elements with a specific key.
Syntax:
map_name.count(val);
Parameters: The multimap::count()
accepts a single parameter:
• val: Key to search for
Return value: The number of elements that have key equal to val
Example of multimap::count() method
#include<iostream> #include<map> using namespace std; int main() { multimap<char,int> m; m.insert( pair<int,int> ('a',1) ); m.insert( pair<int,int> ('b',2) ); m.insert( pair<int,int> ('c',3) ); m.insert( pair<int,int> ('a',4) ); cout<<"Number of elements with key 'a' are: "<< m.count('a'); }
Output:
Number of elements with key 'a' are: 2