简体   繁体   English

安全地依赖unordered_map(hashmap)中的初始化值

[英]Safe to rely on a initialised value in an unordered_map (hashmap)

Let's say I wanted to construct an std::unordered_map<char, int> to map the frequency of characters in a string. 假设我想构造一个std::unordered_map<char, int>来映射字符串中字符的频率。 I would do something like 我会做类似的事情

char* myString;
std::unordered_map<char, int> hashmap;
for(char* pch = myString; *pch !=0 ; pch++)
{
     hashmap[*pch]++;
}

This, to me, feels dangerous as how do I know hashmap[*pch]++ will construct an integer in the value of the hashmap entry that starts at 0? 对我来说,这很危险,因为我怎么知道hashmap[*pch]++会在hashmap条目的值(从0开始)中构造一个整数? Where do I find this guarantee (if it exists)? 我在哪里可以找到此保证(如果存在)?

The usual reference for the language and standard library covers this at least if you follow the link to value initialization as Praetorian mentioned. 语言和标准库的常规参考至少在您遵循Praetorian所述的指向值初始化的链接时涵盖了此内容 See also Is the value of primitive types in std::map initialized? 另请参见std :: map中的原始类型的值是否已初始化? (for which the answer is the same). (其答案是相同的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM