我的代码中有一些内容:
class Foo {
// something
};
std::unordered_map<int, Foo> foo_map;
// some insertion on the map
auto iter = foo_map.find(1);
auto foo = &iter->second; //assume iter!=foo_map.end()
// a lot of operation on the map, may need the rehashing....
所以我的问题是在所有这些操作之后指针foo是否仍然无效?
答案 0 :(得分:3)
大多数情况下,unordered_map中的迭代器在插入/删除后仍然有效。唯一的例外是当集装箱的增长迫使改造时。
元素的引用/指针在所有情况下都保持有效,即使在重新散列后也是如此。
答案 1 :(得分:1)
查看unordered_map
的{{3}}:
存储在容器中的密钥或数据的引用和指针 只有通过擦除该元素才会失效,即使是 相应的迭代器无效。