im wondering if you can use the STL binary_search on STL map. I've tried but still cant get it to work
map<int,string> dataMap;
if(binary_search (dataMap.begin().first, dataMap.end().first, key))
// do some stuff
Thanks in advance! :)
STL map
is inherently a binary search tree - just use map::find
. Using container member functions, where they are present, is preferable to algorithms.
Use std::map::lower_bound
, std::map::find
and std::map::upper_bound
instead.
if(binary_search (dataMap.begin().first, dataMap.end().first, key))
binary_serach requires iterator. dataMap.begin().first
and dataMap.end().first
are not iterators. Another problem is that accessing dataMap.end().first
is very likely to crash your application.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.