繁体   English   中英

使用上限和下限搜索地图

[英]Searching a map with upper bound and lower bound

STL新手问题:

关于函数std::map::upper_boundstd::map::lower_bound是否有效指定地图中实际不存在的键?

std::map<int,int> intmap;
std::map<int,int>::iterator it1, it2;

intmap[1] = 10;
intmap[2] = 20;
intmap[4] = 40;
intmap[5] = 50;

it1 = intmap.lower_bound (3);  // Is this valid?
it2 = intmap.upper_bound (3);  // Is this valid?

是的,它们都是有效的。

map::lower_bound返回一个迭代器,指向第一个不小于key的元素。

map::upper_bound返回一个迭代器,指向第一个大于key的元素。

intmap[1]=10;
intmap[2]=20;
intmap[4]=40;   // <<---both lower_bound(3)/upper_bound(3) will points to here
intmap[5]=50;

lower_bound/upper_bound返回插入值的位置

注意,如果要检查值键是否为map,则可以使用std :: map :: find

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM