std::unordered_map pointers/reference invalidation

倖福魔咒の 提交于 2019-11-30 18:12:43

It looks like v1 can be safely used after inserting new values, even if re-hashing might occur during insertion.

Yes, std::unordered_map::operator[] doesn't invalidate references, even rehashing happens.

(emphasis mine)

If an insertion occurs and results in a rehashing of the container, all iterators are invalidated. Otherwise iterators are not affected. References are not invalidated.

From the standard, [unord.req]/9:

(emphasis mine)

Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!