std :: unordered_map插入仅无效的迭代器,而不是引用和指示元素节点
有人可以解释为什么插入 std :: unordered_map
容器仅无效迭代器,而不会导致参考和指示。另外,我无法理解以下语句从 https:// en。 cppreference.com/w/cpp/container/unordered_map/insert 均值
如果插入成功,则指针和对在节点句柄中保存时获得的元素的指示无效,并且在提取该元素之前获得的指针和参考已成为有效。
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
插入
unordered_map
不会无效引用,因为它不会移动数据,但是基础数据结构可能会发生很大变化。未指定其确切实现的详细信息,并且不同的编译器以不同的方式进行。例如,MSVC具有用于数据存储的链接列表,我相信是查找的向量。插入
可能会导致重新进行,这意味着查找会完全更改,并且链接的列表得到了重大重新定论 - 但原始数据没有移动。迭代器指的是这种基本结构,其任何更改都可能导致迭代器无效。基本上,它们包含的信息比指针和参考文献更多,随后可能会无效。
令人困惑的段落大约是
insert
node_type
- 先前提取的节点。查看方法unordered_map :: extract
由于某种原因,在提取节点时禁止使用指示/参考。插入后,允许再次使用它们。我不知道为什么是这样。
Insertion of
unordered_map
doesn't invalidate references because it doesn't move the data, however the underlying data structure might change rather significantly. Details of how exactly it is implemented aren't specified and different compilers do it differently. For instance, MSVC has a linked list for data storage and, I believe, a vector for the look-up. Andinsert
can cause rehash, meaning look-up gets changed completely and the linked list gets reorded significantly - but original data is not moved.The iterators refer to this underlying structure and any changes to it can cause iterators to invalidate. Basically, they contain more info than pointers and references and subsequently can get invalidated.
The confusing passage is about
insert
of anode_type
- nodes that were previously extracted. Checkout the methodunordered_map::extract
https://en.cppreference.com/w/cpp/container/unordered_map/extract
For some reason it is forbidden to use pointers/references while the node is extracted. After inserting it is allowed to use them again. I don't know why it is so.
就问题的第二部分而言,它指的是“ noreferrer”> node hande 在C ++ 17中引入。它是一种仅动作的类型,它具有基础钥匙和价值的直接所有权。它可以用于更改元素的密钥,而无需重新分配和传输元素所有权,而无需复制或移动。
由于允许它更改类似const的数据(例如键),因此我个人认为,只有在以节点形式为IE时,才允许使用该编辑在隔离容器时进行编辑;这就是为什么指针和对其基础数据的引用一旦将其插入到容器中后,应将其无效。
同样,由于插入不会产生任何重新分离,一旦将节点插入到容器,指针和参考文献中,这些引用在数据被提取之前指向数据将再次有效。
In terms of the second part of the question, it is referring to the Node handle introduced in C++17. It is a move-only type, that has direct ownership of the underlying key and value. It can be used to change the key of an element without reallocation and transfer element ownership without copy or move.
Since it's allowed to change const-like data(such as key), I personally think it makes sense to only allow such edit to happen when it is isolated from the container, ie when it is in the node form; which is why pointer and reference to it underlying data should be invalidated once they are insert back to the container.
Similarly, since the insertion does not incur any reallocations, once the node is inserted back to the container, pointer and references that were point to the data before they were extract will be valid again.