结束迭代器失效规则

2022-01-10 00:00:00 iterator c++

关于this question on iterator invalidation rules,显然标准的精神意味着,例如,双端队列中间的擦除使所有迭代器和对双端队列元素的引用无效"也指的是结束迭代器.

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.

但是,我找不到标准明确说明这一点的任何地方,严格来说,结束迭代器不是容器中 元素 的迭代器.

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.

2003 年标准是否在某处明确说明了这一点?

Does the 2003 standard make this clear somewhere?

推荐答案

例如23.1/10:

没有 swap() 函数使任何引用、指针或迭代器引用被交换的容器的元素无效.[ 注意: end() 迭代器不引用任何元素,因此它可能会失效.――尾注]

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 :/

正如评论中所说,我想这是为了允许结束迭代器指向容器内的标记值.

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

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

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.

相关文章