multimap find( ) in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of multimap::find() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::find()?

multimap::find( ) an inbuilt function in C++ STL, which is defined in <map> header file. find() searches elements in the container which are associated with key K. This function returns an iterator pointing to the single element in a container. It returns an iterator if the element found in the container.

Syntax

iterator multimap_name.find(key);

Parameters

It accepts one parameter key which specifies the key to be searched in the container.

Returns value

This function returns an iterator which refers to the position where the key is present in the container.

Input 

multimap<char, int > newmap;
newmap.insert(make_pair(‘A’, 22));
newmap.insert(make_pair(‘B’, 78));
newmap.insert(make_pair(‘C’, 66));
newmap.insert(make_pair(‘D’, 81));
newmap.insert(make_pair(’E’, 43));
newmap.find(‘D’);

Output 

81

Input 

multimap<char, int > newmap;
newmap.insert(make_pair(1, 15));
newmap.insert(make_pair(2, 18));
newmap.insert(make_pair(3, 45));
newmap.insert(make_pair(4, 66));
newmap.find(4);

Output 

66

Approach can be followed

  • First we initialize the Map.

  • Then we insert the element with Key.

  • Then we find the position of Key using mapfind function( ).

  • Then we print the desired key with its element.

By using the above approach we can find any key in the container, we can also find the position of key in a range.

Example

#include<iostream.h>
#include<map.h>
Using namespace std;
int main( ){
   Multimap<char, int> mp;
   / / inserting the element
   mp.insert({‘b’, 23});
   mp.insert({‘a’, 46});
   mp.insert({‘c’, 78});
   mp.insert({‘e’, 11});
   mp.insert({‘d’, 34});
   cout<< “ The Key value after key c : \n” ;
   cout<< “ Key\t Element”;
   for(auto i = mp.find(‘c’); i != mp.end( ); i++)
      cout<<i-first<< “\t” << i->second << ‘\n’;
   return 0;
}

Output

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

KEY ELEMENT
c 78
d 34
e 11

Example

#include<iostream.h>
#include<map.h>
Using namespace std;
int main( ){
   Multimap<char, int> mp;
   / / inserting the element
   mp.insert({‘1’, 33});
   mp.insert({‘2’, 66});
   mp.insert({‘3’, 55});
   mp.insert({‘4’, 11});
   mp.insert({‘5’, 44});
   cout<< “ The Key value after key 4 : \n” ;
   cout<< “ Key\t Element”;
   for(auto i = mp.find(‘4’); i != mp.end( ); i++)
      cout<<i-first<< “\t” << i->second << ‘\n’;
   return 0;
}

Output

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

KEY ELEMENT
4 11
5 44
raja
Published on 13-Aug-2020 06:04:41
Advertisements