map< int,int>默认值 [英] map<int,int> default values

查看:230
本文介绍了map< int,int>默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::map<int,int> mapy;
++mapy[5];

是否可以假定mapy [5]总是1?我的意思是,在我的代码中,mapy [5]总是得到'++'之前的默认值0,即使没有明确声明。

Is it safe to assume that mapy[5] will always be 1? I mean, will mapy[5] always get the default value of 0 before '++', even if not explicitly declared, as in my code?

p>

推荐答案

使用[]运算符访问地图时,如果键不存在,则会被添加。 int类型的默认初始化器被调用 - 所以它的值为0.

As soon as you access the map with the [] operator, if the key doesn't exist it gets added. The default initializer of the int type gets invoked - so it will get a value of 0.

这篇关于map&lt; int,int&gt;默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆