结束迭代器无效规则 [英] End iterator invalidation rules

查看:132
本文介绍了结束迭代器无效规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于迭代器无效规则的这个问题,很显然,标准的精神意味着,例如,deque中间的擦除使所有迭代器无效并且对deque的元素的引用也指代结束迭代器。

Regarding this question on iterator invalidation rules, it seems obvious that the spirit of the standard means, for example, that "an erase in the middle of the deque invalidates all the iterators and references to elements of the deque" also refers to the end iterator.

然而,I不能在标准的任何地方找到这个显式,严格地说,结束迭代器不是一个元素在容器中的迭代器。

However, I can't find anywhere that the standard makes this explicit, and strictly speaking the end iterator is not an iterator to an element in the container.

推荐答案

例如,23.1 / 10:

For example, 23.1/10:


swap()函数使任何引用,指针或迭代器引用要交换的容器的元素无效。 [注意: end()迭代器不引用任何元素,因此它可能无效。 -end note]

no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped. [ Note: The end() iterator does not refer to any element, so it may be invalidated. —end note ]

我不知道是否可以确定迭代器引用元素在标准中一直使用排除结束迭代器:/

I do not know if we can be certain that iterator referring to an element has been used consistently in the Standard to exclude end iterators :/

正如在注释中所说的,我想这是允许结束迭代器指向容器内的sentinel值。

As said in a comment, I suppose this is to allow end iterators pointing to sentinel values within the container.

例如,一个典型的双向链接 List 实现是创建一个 Node 结构,在 List 中有一个 Node 作为结束节点。

For example, a typical doubly linked List implementation is to create a Node structure, and have one Node by value within the List to act as the end node.

这篇关于结束迭代器无效规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆