c++ STL地图在创建后移动一个值的位置?

发布于 2025-01-23 18:05:41 字数 127 浏览 0 评论 0原文

我在这里和那里阅读了一些提示,即将对象插入C ++ STL映射后,只要一个不删除它,它在内存中的位置就永远不会更改。但是没有人提到任何文献或来源来支持它,所以我不知道这种提示有多可靠。谁能确定/可靠地回答这个问题?可以依赖实施吗?有保证吗?

I have read some hints here and there that after inserting an object into a c++ stl map, then as long as one doesn't delete it, its location in memory never changes. But nobody ever mentioned any literature or sources to back it up, so I don't know how reliable such hints are. Can anyone answer this definately/reliably? Could it be implementation-dependent? Is there a guarantee anywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-01-30 18:05:41

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

不。

任何人都可以确定/可靠地回答吗?

您可以依靠它。

是否取决于实现?

它不能取决于实施。

是否可以在任何地方保证?

是的,它可以在C ++标准中保证:

[container.rev.reqmts]

除非另有指定(无论是明确或通过以其他函数定义函数),请调用容器成员函数或将容器作为参数传递到库函数,不得使迭代器无效或更改对象的值或更改对象的值在该容器中。

[cosidiative.reqmts.general]

插入,插入_range和Emplace成员不得影响迭代器的有效性和对容器的参考,擦除成员应仅使迭代器无效,并引用了对擦除的元素的参考。

提取成员仅将迭代器无效到删除的元素;指针和对删除元素的引用仍然有效。
但是,在元素由node_type拥有时,通过此类指针和引用访问元素是未定义的行为。
如果成功插入该元素,则引用和指示元素在node_type拥有时获得的元素无效。

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

No.

Can anyone answer this definately/reliably?

You can rely on it.

Could it be implementation-dependent?

It couldn't be dependent on implementation.

Is there a guarantee anywhere?

Yes, it is guaranteed in the C++ standard:

[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文