【发布时间】:2016-08-16 06:21:28
【问题描述】:
对于下面的示例程序,lower_bound 正在生成核心转储。知道哪里可能出错了吗?
另外,谁能解释一下lower_bound和upper_bound算法在c++中是如何工作的?
#include <iostream>
#include <map>
int main ()
{
std::map<int,int> mymap;
std::map<int,int>::iterator itlow,itup;
mymap[100]=10;
mymap[1000]=20;
mymap[2000]=40;
mymap[3000]=60;
mymap[4000]=80;
mymap[5000]=100;
itlow=mymap.lower_bound (2001);
itup=mymap.upper_bound (1500);
std::cout<<(itlow->first)<<std::endl;
std::cout<<(itup->first)<<std::endl;
mymap.erase(itlow,itup); // erases [itlow,itup)
// print content:
for (std::map<int,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
return 0;
}
【问题讨论】:
-
算法是如何工作的:关于它们的接口,请咨询suitable reference。它们如何在内部工作:地图(很可能)是二叉搜索树,因此找到这样的边界是对这种树的标准遍历。