C++ Standard Library C++ STL Library

C++ multimap - find() Function



The C++ 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 multimap::end.

Syntax

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
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 multimap::end if k is not found.

Time Complexity

Logarithmic i.e, Θ(log(n)).

Example:

In the example below, the multimap::find function is used to find the key in the multimap called MyMMap.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  multimap<string, string> MyMMap;
  multimap<string, string>::iterator it;

  MyMMap.insert(pair<string, string>("USA", "Washington"));  
  MyMMap.insert(pair<string, string>("CAN", "Toronto"));
  MyMMap.insert(pair<string, string>("IND", "Delhi"));
  MyMMap.insert(pair<string, string>("GBR", "London"));
  MyMMap.insert(pair<string, string>("IRL", "Dublin"));

  it = MyMMap.find("IND");
  MyMMap.erase(it);
  MyMMap.erase(MyMMap.find("IRL"));

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

  return 0;
}

The output of the above code will be:

MyMMap contains: 
 CAN  Toronto
 GBR  London
 USA  Washington

❮ C++ <map> Library