我有无序的计数器 map ,例如:
std::unordered_map<std::string, std::size_t> counters_;
在尝试增加值之前我是否需要手动创建一个值?下一行会被视为未定义行为吗?
std::unordered_map<std::string, std::size_t> counters_;
counters_["non_existing_key"] += 1;
最佳答案
通过使用std::map::operator[]
,您将在 map 中创建一个新值(如果之前不存在)。
此外,新值将是 value-initialized ,因此增加值是明确定义的。
关于c++ - 增加无序映射中默认构造的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69335705/