c++ 中关于映射的初始值假设
我正在初始化地图 map
在C++中。我在 stl 中找不到 contains_key 或类似的函数, 因此我只是使用 ex[inputString]++;
调试器显示 int 正确初始化为零,这是一个很好的假设吗?
I am initializing a map map<string,int> ex;
in C++. I could not find contains_key or similar function in stl,
hence I am just using ex[inputString]++;
The debugger shows the int to be initialized to zero correctly, is it a good assumption?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用
operator[]
访问时不存在的值是默认构造的。对于数值,这是 0。也就是说,您正在寻找
count
方法:Yes, values that do not exist when accessed with
operator[]
are default-constructed. For numeric values, this is 0.That said, you're looking for the
count
method: