0

can someone please explain lower_bound function in map stl in c++?i am confused in this example given on c++ site

// map::lower_bound/upper_bound #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator itlow,itup; mymap['a']=20; mymap['b']=40; mymap['c']=60; mymap['d']=80; mymap['e']=100; itlow=mymap.lower_bound ('b'); // itlow points to b itup=mymap.upper_bound ('d'); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) // print content: for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; return 0; }

13th Aug 2018, 11:59 AM
Bahubali
Bahubali - avatar
1 Resposta
+ 1
Bahubali as you might be aware that map has key and value... if you do lowebound on map, it searches for key and returns iterator based on value as per below logic : for example, map has key as 1,3,4 and 5.. now, lower_bound (2) will give you iterator of key as 3 because 1 is not equal to or more than searched value (2 in our case) lower_bound (3) will give you iterator of key as 3 because 1 is not equal to or more than searched value (3 in our case), but 3 is equal to 3
13th Aug 2018, 1:10 PM
Ketan Lalcheta
Ketan Lalcheta - avatar