关于c ++中映射的初始值假设 [英] An initial value assumption about map in c++

查看:92
本文介绍了关于c ++中映射的初始值假设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在初始化地图 map< string,int> ex;
。我找不到contains_key或类似的函数在stl,
因此我只是使用 ex [inputString] ++;

调试器显示要正确初始化为零的int是否是一个好的假设?

The debugger shows the int to be initialized to zero correctly, is it a good assumption?

推荐答案

使用 operator [] 访问时不存在的值是缺省构造的。对于数值,这是0.

Yes, values that do not exist when accessed with operator[] are default-constructed. For numeric values, this is 0.

也就是说,你正在寻找 count p>

That said, you're looking for the count method:

bool hasElement = ex.count("element");

这篇关于关于c ++中映射的初始值假设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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