开发者

An initial value assumption about map in c++

开发者 https://www.devze.com 2022-12-21 05:13 出处:网络
I am initializing a map map<string,int> ex; in C++. I could not find conta开发者_运维百科ins_key or similar function in stl,

I am initializing a map map<string,int> ex; in C++. I could not find conta开发者_运维百科ins_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?


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

精彩评论

暂无评论...
验证码 换一张
取 消