【发布时间】:2020-12-11 12:27:03
【问题描述】:
我在下面有这个函数,wordcount_map 是一个 std::unordered_map <:string int> 类型。我正在使用 count 函数来查看键是否在地图中,以及是否要返回存储在该键处的值。但是,我收到一个错误,因为 map 被标记为 const 并且不允许使用 [] 运算符。请指教!
int lookupWithFallback(const StringIntMap& wordcount_map, const std::string& key, int fallbackVal) {
if (wordcount_map.count(key)){
return wordcount_map[key];
}
else {
return fallbackVal;
}
}
【问题讨论】:
标签: c++ unordered-map