加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 搜索具有上限和下限的地图

发布时间:2020-12-16 07:53:05 所属栏目:百科 来源:网络整理
导读:STL新手问题: 关于函数std :: map :: upper_bound和std :: map :: lower_bound是否有效指定地图中实际不存在的键? 例 std::mapint,int intmap;std::mapint,int::iterator it1,it2;intmap[1]=10;intmap[2]=20;intmap[4]=40;intmap[5]=50;it1=intmap.lower_b
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将点返回指向不小于键的第一个元素的迭代器.

map::upper_bound返回指向大于键的第一个元素的迭代器.

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返回值将被插入的位置.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读