map::upper_bound function


The map::upper_bound function purpose is to find the last element where the element will get inserted.

iterator upper_bound(const key_type& x);

Parameters:
x – The key of the element which is to be inserted into the container.

Return type
iterator – An iterator pointing to the subsequence element in the container.If the key supplied is larger than any key of the elements in the container end() is returned.

Link:key_type of pair

To keep it simple the upper_bound returns an element whose key>x(argument passed).So it always return the subsequence element in the container.

Code example

map<int,string>mp{{5,”Five”} , {10 , “Ten”} , { 90 , “Ninety”} , { 901 , “Nine-hundred and One”}};

map<int,string>::iterator it1=mp.upper_bound(90) ,
it2=mp.upper_bound(900) ;

cout<<it1->first << ” ” << it1->second << endl
<< it2->first << ” ” << it2->second ;

Output

901 Nine-hundred and One
901 Nine-hundred and One


 


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