关联容器中std :: array data()失效的风险?

时间:2016-07-11 18:53:14

标签: c++ c++11

请考虑以下事项:

std::unordered_map<int, std::array<float,50>> foo;
...
auto pointerToArray = foo.at(3).data();

我已阅读thisthis。我有以下问题:

  • 1)pointerToArray在提及时属于哪个类别 失效规则,迭代器或引用?
  • 2)pointerToArray将失效的风险是什么? (假设foo中的配对密钥未被删除)?
  • 3)这些答案中有什么区别(如果有的话) unordered_mapmap之间的问题?

vector不同,array本身不会重新分配,因此不存在自行更改内存地址的风险,但由于它位于无序/地图内,因此情节变浓

更新:找到another question表示在这种情况下不存在失效的风险。

1 个答案:

答案 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.