我想知道你是否可以在 STL 地图上使用 STL binary_search。我已经尝试过,但仍然无法正常工作
map<int,string> dataMap;
if(binary_search (dataMap.begin().first, dataMap.end().first, key))
// do some stuff
提前致谢!:)
STLmap本质上是一个二叉搜索树 - 只需使用map::find. 在存在容器成员函数的地方使用它们比算法更可取。
使用std::map::lower_bound,std::map::find和std::map::upper_bound代替。
if(binary_search (dataMap.begin().first, dataMap.end().first, key))
binary_serach 需要迭代器。dataMap.begin().first并且dataMap.end().first不是迭代器。另一个问题是访问dataMap.end().first很可能会使您的应用程序崩溃。