map::find function


The map::find function tries to find the element with the specified key.

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

key_type:An alias of the key type in the pair element.

Link:key_type of pair

First function
Parameters:
x – A key of the pair element to be located.

Return type
iterator -A read and write iterator which points to the pair with the specified key.If no pair with the given key is found end() is returned.

Second function
Parameters:
x – A key of the pair element to be located.

Return type
iterator -A read only iterator which points to the pair with the specified key.If no pair with the given key is found end() is returned.

Let’s look at an example that utilized the find() function to locate the pair with the given key.

Code example

map<int,char> m{{ 23 , ‘2’} , {90 , ‘9’} , {88 , ‘8’}};

if( m.find(50) != m.end() )
{
 cout<< “Key found”;
}
else
  cout<< “Key not found”;

Output

Key not found

A pair element with the key 50 does not exist hence the statement under ‘else’ is executed.

Here is another code example to test the return type of the function.

Code example

map<int,char>::iterator mRet=m.find(90);

mRet->second ='(‘ ;

cout< mRet->first << ” ” << mRet->second << endl;

Output

90 (


 


Stop wasting time,earn money($$$) from your website-Join Now!