C ++ deque:当迭代器无效时 [英] C++ deque: when iterators are invalidated

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

问题描述

如果我错了,请纠正我。谢谢!

Please correct me if I am wrong. Thank you!

插入擦除 ,但是插入/删除位置之前的元素不会重新定位,因此它们的迭代器仍然有效。

insert and erase will relocate elements, but elements before the position where insertion/erasure takes place don't relocate and hence their iterators remain valid.

push_back pop_back 不会使任何迭代器无效。

push_back and pop_back don't invalidate any iterators.

push_front pop_front 无效所有迭代器。

swap 不会重定位元素,但不知何故我认为它应该无效的迭代器。

swap won't relocate elements, but somehow I think it should invalidate iterators.

推荐答案

push_back 和 push_front() insert()的形式定义。同样, pop_back() pop_front() / code>。

push_back() and push_front() are defined in terms of insert(). Similarly, pop_back() and pop_front() are defined in terms of erase().

这里是C ++ 03标准中关于 insert()的迭代器无效的说法(23.2.1.3/1):

Here's what the C++03 standard says about iterator invalidation for insert() (23.2.1.3/1):


插入代码中间的所有迭代器和
引用元素的deque。在
deque的任一端插入一个无效的所有迭代器到deque,但对
没有影响引用deque的元素的有效性。

An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque.

因此 push_front() push_back() 无效的迭代器,但引用元素本身仍然有效。

So push_front() and push_back() will invalidate iterators, but references to the elements themselves remain valid.

对于 erase() end(23.2.1.3/4):

For erase() at either end (23.2.1.3/4):


deque中间的擦除使所有迭代器无效,
引用元素的deque。在
deque的任一端的擦除仅使迭代器和对已擦除的
元素的引用无效。

An erase in the middle of the deque invalidates all the iterators and references to elements of the deque. An erase at either end of the deque invalidates only the iterators and the references to the erased elements.

因此 pop_front() pop-back()只会使迭代器无效/ 。

So pop_front() and pop-back() only invalidate iterators/references to the element at the end in question.

对于任何标准容器(23.1 / 10容器需求),这是关于 swap() ):

And this is said says this about swap() for any standard container (23.1/10 "Container requirements"):


否swap()函数使任何引用,指针或迭代器无效
引用容器的元素。

no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.

C ++ 11增加了关于 end() iterator on a deque 表现为这些操作。基本上, end()之后的迭代器应该在 swap()之后或者在擦除最后一个元素 deque

C++11 adds the following clarifications regarding how the end() iterator on a deque behaves for these operations. Basically, an iterator to end() should be treated as invalid after a swap() or after erasing the last element in the deque:


擦除操作擦除deque的最后一个元素

An erase operation that erases the last element of a deque invalidates only the past-the-end iterator and all iterators and references to the erased elements.

每个迭代器引用一个容器中的一个元素在交换之前应指交换后另一个集装箱中的同一元素。未指定在交换之前具有值a.end()的迭代器在交换之后是否具有值b.end()。

Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap. It is unspecified whether an iterator with value a.end() before the swap will have value b.end() after the swap.

我认为,即使你还没有使用C ++ 11编译器,编写代码也是一个好主意。

I think it would be a good idea to code as if these rules apply even if you're not yet using a C++11 compiler.

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

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