[英]Searching a map<pair<int, int>, ...> with lower and upper bound
[英]Searching a map with upper bound and lower bound
STL新手问题:
关于函数std::map::upper_bound
和std::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.