请考虑以下事项:
std::unordered_map<int, std::array<float,50>> foo;
...
auto pointerToArray = foo.at(3).data();
pointerToArray
在提及时属于哪个类别
失效规则,迭代器或引用?pointerToArray
将失效的风险是什么?
(假设foo
中的配对密钥未被删除)?unordered_map
和map
之间的问题?与vector
不同,array
本身不会重新分配,因此不存在自行更改内存地址的风险,但由于它位于无序/地图内,因此情节变浓
更新:找到another question表示在这种情况下不存在失效的风险。
答案 0 :(得分:1)
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.