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?
Run Code Online (Sandbox Code Playgroud)
bil*_*llz 27
是的,它们都是有效的.
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;
Run Code Online (Sandbox Code Playgroud)
lower_bound/upper_bound返回值插入的位置.
注意,如果要检查值键是否为map,则可以使用std :: map :: find
| 归档时间: |
|
| 查看次数: |
17301 次 |
| 最近记录: |