映射< INT,INT>默认值 [英] map<int,int> default values
本文介绍了映射< 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?
推荐答案
只要使用[]运算符访问地图,如果该键不存在,则会被添加。 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.
这篇关于映射< INT,INT>默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文