简体   繁体   English

C++ deque:当迭代器失效时

[英]C++ deque: when iterators are invalidated

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. inserterase将重新定位元素,但在插入/擦除发生位置之前的元素不会重新定位,因此它们的迭代器仍然有效。

push_back and pop_back don't invalidate any iterators. push_backpop_back不会使任何迭代器失效。

push_front and pop_front invalidate all iterators. push_frontpop_front使所有迭代器无效。

swap won't relocate elements, but somehow I think it should invalidate iterators. swap不会重新定位元素,但我认为它应该使迭代器无效。

push_back() and push_front() are defined in terms of insert() . push_back()push_front()是根据insert()定义的。 Similarly, pop_back() and pop_front() are defined in terms of erase() .同样, pop_back()pop_front()是根据erase()定义的。

Here's what the C++03 standard says about iterator invalidation for insert() (23.2.1.3/1):以下是 C++03 标准关于insert() (23.2.1.3/1) 的迭代器失效的说明:

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.在双端队列的任一端插入会使双端队列的所有迭代器无效,但对双端队列元素引用的有效性没有影响。

So push_front() and push_back() will invalidate iterators, but references to the elements themselves remain valid.所以push_front()push_back()迭代器失效,但对元素的引用本身仍然有效。

For erase() at either end (23.2.1.3/4):对于任一端的erase() (23.2.1.3/4):

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.双端队列两端的擦除只会使迭代器和对被擦除元素的引用无效。

So pop_front() and pop_back() only invalidate iterators/references to the element at the end in question.因此, pop_front()pop_back()只会使对相关末尾元素的迭代器/引用无效。

And this is said says this about swap() for any standard container (23.1/10 "Container requirements"):据说这是关于任何标准容器的swap() (23.1/10“容器要求”):

no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped.没有 swap() 函数使任何引用、指针或迭代器无效,这些引用、指针或迭代器引用了被交换的容器的元素。

C++11 adds the following clarifications regarding how the end() iterator on a deque behaves for these operations. C++11 添加了以下关于deque上的end()迭代器对这些操作的行为的说明。 Basically, an iterator to end() should be treated as invalid after a swap() or after erasing the last element in the deque :基本上,应将end()的迭代器在swap()或擦除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.擦除双端队列最后一个元素的擦除操作只会使尾后迭代器和所有迭代器以及对被擦除元素的引用无效。

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.未指定交换前值为 a.end() 的迭代器在交换后是否具有值 b.end() 。

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++11 编译器,也可以像这些规则一样进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM