An initial value assumption about map in c++

梦想与她 提交于 2019-12-30 08:30:47

问题


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?


回答1:


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");


来源:https://stackoverflow.com/questions/2346481/an-initial-value-assumption-about-map-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!