<分区>
一旦一个键值对被插入到一个 unordered_map 中,假设 iter 指向这个对,&iter->first 是否会在这个对存在于 unordered_map 的整个生命周期中保持不变?
键大多是长字符串,所以我想将它们的 iterator->first 地址存储在其他引用位置而不是字符串本身。
<分区>
一旦一个键值对被插入到一个 unordered_map 中,假设 iter 指向这个对,&iter->first 是否会在这个对存在于 unordered_map 的整个生命周期中保持不变?
键大多是长字符串,所以我想将它们的 iterator->first 地址存储在其他引用位置而不是字符串本身。
最佳答案
没关系,除非你直接删除元素。
对于 std::unordered_map::insert
,
If rehashing occurs due to the insertion, all iterators are invalidated. Otherwise iterators are not affected. References are not invalidated. Rehashing occurs only if the new number of elements is greater than
max_load_factor()*bucket_count()
.
对于 std::unordered_map::erase
,
References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated.
关于c++ - unordered_map::iterator->first 的地址是否会随着后续的插入和删除而改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495963/