假设我有以下功能:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> map(26);
for (int i = 0; i < magazine.size(); ++i)
++map[magazine[i]];
for (int j = 0; j < ransomNote.size(); ++j)
if (--map[ransomNote[j]] < 0)
return false;
return true;
}
现在假设 ransomNote
有一个 map 中不存在的元素,我通过阅读文档和问题来理解:
What happens if I read a map's value where the key does not exist?
构造了一个新的默认键值 ' '
。现在在第二个 for 循环中引用键时,值如何初始化为零?
键对应的值的变化是如何发生的?
是否有相同的文档?
最佳答案
A new default key is constructed having the value
' '
.
不,那是错误的。 char
的“默认” 值为0
,对于任何其他整数数字类型也是如此。
Is there any documentation for the same?
std::map::operator[]
的行为被详细记录了 here .
关于c++ - key不存在时map给出的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45129642/