C++ STL Map 是否会在创建后移动值的位置?

Does a C++ STL Map move a value's location around after creation?

我在这里和那里看到了一些提示,在将一个对象插入到 c++ stl 映射中之后,只要不删除它,它在内存中的位置就永远不会改变。但是从来没有人提到任何文献或资料来支持它,所以我不知道这样的暗示有多可靠。谁能回答这个definately/reliably?它可以依赖于实现吗?哪里有保证?

Does a C++ STL Map move a value's location around after creation?

没有

Can anyone answer this definately/reliably?

你可以放心。

Could it be implementation-dependent?

它不能依赖于实现。

Is there a guarantee anywhere?

是的,在C++标准中是有保证的:

[container.rev.reqmts]

Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container.

[associative.reqmts.general]

The insert, insert_­range, and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.

The extract members invalidate only iterators to the removed element; pointers and references to the removed element remain valid. However, accessing the element through such pointers and references while the element is owned by a node_­type is undefined behavior. References and pointers to an element obtained while it is owned by a node_­type are invalidated if the element is successfully inserted.