gpt4 book ai didi

c++ - 指向 map 中变量的指针会失效吗?

转载 作者:搜寻专家 更新时间:2023-10-31 01:04:53 24 4
gpt4 key购买 nike

我的代码中有一些东西:

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 是否仍然无效?

最佳答案

大多数情况下,unordered_map 中的迭代器在插入/删除后仍然有效。唯一的异常(exception)是当容器的增长迫使重新哈希时。

元素的引用/指针在所有情况下仍然有效,即使在重新散列之后也是如此。

关于c++ - 指向 map 中变量的指针会失效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23237268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com