C++ unordered_multimap::find() Function



The C++ std::unordered_multimap::find() function is used to finds an element associated with key k. If operation succeeds then methods returns iterator pointing to the element otherwise it returns an iterator pointing the multimap::end().

Syntax

Following is the syntax of std::unordered_multimap::find() function.

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

Parameters

  • k − It indicates the key to be searched.

Return value

If the specified key is found, then an iterator pointing to the element is returned; otherwise, a multimap::end() iterator is returned.

Example 1

In the following example, we are demonstrating the usage of the unordered_multimap::find() function.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> umm = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'c', 3},
      {'d', 6},
      {'e', 5}
   };
   auto it = umm.find('d');
   cout << "Iterator points to " << it->first
      << " = " << it->second << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

Iterator points to d = 6

Example 2

Consider the following example, where we are going to find the key/value pairs whose values are even.

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_multimap<char, int> um = {
      {'a', 1},
      {'b', 2},
      {'c', 3},
      {'d', 4},
      {'e', 5},
      {'f', 6},
   };
   for(auto it = um.begin(); it!=um.end(); ++it){
      if(it->second % 2 == 0){
         it = um.find(it->first);
         cout<<it->first<<" = "<<it->second<<endl;
      }
   }
   return 0;
}

Output

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

f = 6
d = 4
b = 2

Example 3

Let's look at the following example, where we are going to create a input string that accepts input string, if the input key is available in the multimap, then it returns their key-values otherwise not found.

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_multimap<string,double> umm = {
      {"John",55.4},
      {"Vaibhaw",65.1},
      {"Sunny",50.9},
      {"John",60.4}
   };
   string input;
   cout << "who? ";
   getline (cin,input);

   auto got = umm.find (input);

   if ( got == umm.end() )
      cout << "not found";
   else
      cout << got->first << " is " << got->second;
   return 0;
}

Output

Following is the output when our input is available in the multimap.

who? John
John is 60.4

Output

Following is the output when the input is not available −

who? Aman
not found

Example 4

Following is the example, where we are going to use the find() function to search an element for the specified key.

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main () {
   unordered_multimap<string,int> umm = {
      {"John",1},
      {"Vaibhaw",2},
      {"Sunny",3},
      {"John",4}
   };
   if (auto it = umm.find("John"); it != umm.end())
      cout << "Found " << it->first << " " << it->second << '\n';
   else
      cout << "Not found\n";
   return 0;
}

Output

Output of the above code is as follows −

Found John 4
Advertisements