C++ Standard Library C++ STL Library

C++ unordered_multimap - find() Function



The C++ unordered_multimap::find function is used to search the container for a specified key and returns the iterator to it if found, else returns the iterator to unordered_multimap::end.

Syntax

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

Parameters

k Specify key to search for.

Return Value

An iterator to the key if k is found, or unordered_multimap::end if k is not found.

Time Complexity

Average case: Constant i.e, Θ(1).
Worst case: Linear i.e, Θ(n).

Example:

In the example below, the unordered_multimap::find function is used to find the specified key in uMMap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  unordered_multimap<string, string>::iterator it;

  uMMap.insert(pair<string, string>("USA", "Washington"));  
  uMMap.insert(pair<string, string>("CAN", "Toronto"));
  uMMap.insert(pair<string, string>("IND", "Delhi"));
  uMMap.insert(pair<string, string>("GBR", "London"));
  uMMap.insert(pair<string, string>("IND", "Mumbai"));

  it = uMMap.find("IND");
  uMMap.erase(it);

  cout<<"uMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

uMMap contains: 
 GBR  London
 IND  Delhi
 USA  Washington
 CAN  Toronto

❮ C++ <unordered_map> Library