unordered_map::find() 的一个特性是自动插入我们用 0 值查找的键吗? 让我说清楚
unordered_map<int, int> tempMap;
//some code
if(tempMap.find(1) == tempMap.end()){
//do something but not insert the 1 with a corresponding value into the tempMap
}
所以如果我再次查找 1,它会在 tempMap 中以 0 作为对应值吗? 这是 unordered_map 的一个特性吗?
最佳答案
不,find
只搜索并返回一个迭代器。
也就是说,std::unordered_map
(和 std::map
)都重载了 operator[]
如果需要,将插入默认值:
// if element with a key value one exists, return that. otherwise, insert
// a default initialized element (zero in this case), and return that
auto& val = tempMap[1];
关于c++ - unordered_map::find() 插入查找的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7434797/