关联容器中std :: array数据()无效的风险? [英] Risk of invalidation of std::array data() in associative container?
问题描述
请考虑以下内容:
std::unordered_map<int, std::array<float,50>> foo;
...
auto pointerToArray = foo.at(3).data();
I have read this and this. I have the following questions:
- 1)
pointerToArray
无效规则,迭代器或引用? - 2)
pointerToArray
将会失效的风险
(假设foo
中的配对金钥不会被清除)? - 3)
unordered_map
和地图
之间的这些
问题的答案?
- 1)
pointerToArray
falls into which category when referring to the invalidation rules, iterator or reference? - 2) What are the risks that
pointerToArray
will get invalidated (assuming its paired key infoo
is not erased)? - 3) What, if any, difference is there in the answers to these
questions between
unordered_map
andmap
?
与向量
,数组
本身不会重新分配,因此没有它自己改变内存地址的风险,但因为它在无序/映射内,所以情节变粗。
Unlike vector
, array
itself will not re-allocate and thus there is not a risk of it changing memory addresses on its own, but as it is inside a unordered/map, the plot thickens.
更新:找到另一个问题,表明在这种情况下没有无效的风险。
Update: Found another question that suggests there is no risk of invalidation in this case.
推荐答案
根据您提供的第一个链接提供的信息, pointerToArray
应该不对地图作出,除非你从地图中删除元素本身。 std :: array< float,50>
对象将存储在堆中,并且只有指针(或可能引用,取决于 std :: unordered_map
实现)到该对象将在地图内乱抛垃圾。
Based on the information provided by the first link you provided, pointerToArray
should NOT be invalidated by any subsequent changes made to the map, unless you were to erase the element itself from the map. The std::array<float, 50>
object will be stored in the heap, and only the pointer (or possibly reference, depending on how std::unordered_map
is implemented) to that object will be shuffled around within the map.
这篇关于关联容器中std :: array数据()无效的风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!