map::equal_range function


The map::equal_range function returns the lower bound and upper bound of the element where the key will get inserted.

pair<iterator, iterator> equal_range(const key_type& x);

key_type:type of the key of the element.

Parameters:
x – A key of the element which is to be located.

Return type
pair<iterator, iterator> -A pair with iterator objects whose first value is lower_bound() and the second value is the upper_bound().

Link :map::lower_bound function
Link :map::upper_bound function

Note this function is sensible when using with multimap container.Also it is same as calling make_pair( map.lower_bound(x) , map.upper_bound(x)), but equal_range is faster.

Code example

map<int,string> mp{{56 , “8” } , { 89 , “Y” } , { 143 , “Å” } , { 200 , “╚” }};

pair< map<int,string>::iterator ,map<int,string>::iterator > it=mp.equal_range(100) ,
itNew;=mp.equal_range(89);

cout<< it.first->first << ” ” << it.first->second << endl
<< it.second->first << ” ” << it.second->second;

cout<< “\n\n”;

cout<< itNew.first->first << ” ” << itNew.first->second << endl
<< itNew.second->first << ” ” << itNew.second->second << endl ;

Output

143 Å
143 Å

89 Y
143 Å