gpt4 book ai didi

c++ - key不存在时map给出的值

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:04 24 4
gpt4 key购买 nike

假设我有以下功能:

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com