VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-find-all-occurrences-of-a-key-in-a-multimap-in-cpp/

⇱ How To Find All Occurrences of a Key in a Multimap in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Find All Occurrences of a Key in a Multimap in C++?

Last Updated : 23 Jul, 2025

In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++.

Example:

Input:
myMutimap = {{ "id", "111" }, { "id", "112" }, { "student", "John" },
{ "student", "Bob" }, { "student", "Mike" } };

Output:
Occurrences of the key student are:
John Bob Mike

Finding All Values with Given Key in a Multimap in C++

To find all occurrences of a specific key in a multimap in C++ STL, we can use the std::multimap::equal_range() function, which returns a std::pair of iterators representing the range of elements with the specified key in the multimap.

C++ Program for Finding All Values with Given Key in a Multimap

The below program demonstrates how we can find all occurrences of a specific key in a multimap in C++ STL.


Output
Occurrences of the key 'student' are: 
John Bob Alice Mike 

Time Complexity: O(log N), where N is the size of the multiset.
Auxiliary Space: O(1)



Comment