c++ 中关于映射的初始值假设

发布于 2024-08-23 07:43:50 字数 164 浏览 4 评论 0原文

我正在初始化地图 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨轻弹 2024-08-30 07:43:50

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

也就是说,您正在寻找 count 方法:

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

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:

bool hasElement = ex.count("element");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文