C++ 中的容器失效是什么意思?意思是?
今天我了解了 C++ 容器上下文中的术语“失效”。谁能解释一下这是什么意思?
似乎在循环容器时不允许以某种方式修改容器的元素。但具体是什么方式呢?
请帮助我理解这个主题。
谢谢,博达·西多。
I learned today about the term invalidation
in context of C++ containers. Can anyone explain what it means?
It seems like you're not allowed to modify elements of a container in some way when looping over the container. But what way exactly?
Please help me understand this topic.
Thanks, Boda Cydo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
容器不会失效——引用容器中元素的迭代器会失效。
迭代器是容器内特定项目的句柄。只要该项目保留在容器内,并且容器不会在内部重新排列自身,迭代器就有效。当发生这两件事之一时,迭代器就会失效,因为之后迭代器作为容器的句柄不再有效。
使迭代器无效的最明显的方法是从集合中删除其引用的项,例如:
使迭代器无效的更微妙的方法是使容器在内部重新排列自身(例如重新分配其内部存储)。例如,这可以通过使某些类型的容器扩展来完成:
您可以通过预先保留空间来在某些容器中防止这种情况发生:
每个 STL 容器的文档应描述在什么情况下将或可能发生迭代器失效。
Containers don't become invalidated -- iterators referring to elements in containers become invalidated.
An iterator is a handle to a particular item within a container. The iterator is valid so long as that item remains inside the container, and the container does not internally rearrange itself. An iterator is invalidated when one of those two things happens, since afterwords the iterator is no longer valid as a handle into the container.
The most obvious way to invalidate an iterator is to remove its referred-to item from the collection, e.g.:
The more subtle way to invalidate an iterator is to cause the container to internally rearrange itself (e.g. reallocate its internal storage). This can be done, for example, by causing certain types of containers to expand:
You can prevent this in some containers by pre-reserving space:
The documentation for each STL container should describe under what circumstances iterator invalidation will or might occur.
当底层容器以某种方式发生变化时,某些迭代器会失效。
例如:当容器的大小发生变化时,
vector
迭代器将失效。当底层数据被删除时,list
迭代器将失效。这意味着迭代器不再有效。尝试取消引用它可能会导致异常或未定义的行为。尝试操纵它并不能保证成功。
Certain iterators become invalidated when the underlying container changes in a certain way.
For example:
vector
iterators are invalidated when the size of the container changes.list
iterators are invalidated when the underlying data is deleted.This means that the iterator is no longer valid. Attempts to dereference it may cause exceptions or undefined behavior. Attempts to manipulate it are not guaranteed to work.
如果您有一个对象数组(或列表)并循环遍历它们,您删除了一些项目 - 那么数组中的简单索引可能无效,但迭代器仍然有效。
If you had an array (or list) of objects and looping over them you deleted some items - then a simple index into the array might not be valid, but iterators still would be.
我认为这都是关于迭代器失效的。有几个例子:
I think it is all about iterator invalidating. There few examples :