混淆迭代器无效在deque [英] Confusion on iterators invalidation in deque

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

问题描述

我对deque中的迭代器无效有点困惑。
(在的上下文中问题)

I'm bit confused regarding iterator invalidation in deque. (In the context of this question)

以下是摘录自The C ++ Standard Library:A Tutorial and Reference,
By Nicolai M. Josuttis

Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis


任何插入或删除元素
除了开头或结尾
使所有指针,引用,
和引用deque的元素
的迭代器。

Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterators that refer to elements of the deque.

下面是SGI 网站:


语义的deque的迭代器无效
如下。插入
(包括 push_front push_back
使引用
的所有迭代器到出发队。在
deque中间的擦除使所有迭代器无效,
引用deque。在
处删除deque的开头或结尾(包括
pop_front pop_back
迭代器只有当它指向
已擦除的元素。

The semantics of iterator invalidation for deque is as follows. Insert (including push_front and push_back) invalidates all iterators that refer to a deque. Erase in the middle of a deque invalidates all iterators that refer to the deque. Erase at the beginning or end of a deque (including pop_front and pop_back) invalidates an iterator only if it points to the erased element.

IMHO,deque是第一个块在一个方向生长,最后一个块在相反方向生长。

IMHO, deque is collection of blocks with first block growing in one direction and the last block in opposite direction.

  -   -  -  
  -   -  -
  |   -  -  ^
  |   -  -  |
  V   -  -  |
      -  -  -
      -  -  -

push_back ,push_front 不应该对deque迭代器有任何影响(我同意Josuttis)。

push_back, push_front should not have any impact on deque iterators ( I agree with Josuttis).

正确的解释是什么?这个标准说了什么?

What is the correct explanation? what the standard say on this?

推荐答案

标准工作草案


template< class InputIterator>
void insert(iterator position,
InputIterator first,InputIterator last);

template < class InputIterator > void insert ( iterator position , InputIterator first , InputIterator last );

1效果:插入中间
的deque使所有
迭代器和对deque的元素
的引用无效。 deque的任一端
插入使所有
迭代器无效,但对引用
对deque的元素的有效性没有
的影响。


1 Effects: 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."

因此两者都是正确的。正如Josuttis所指出的,正面或背面的插入不会使对deque的元素的引用无效,

So both are correct. As Josuttis indicates, insertion at the front or back doesn't invalidate references to elements of the deque, only iterators to the deque itself.

编辑:A 更新的草稿基本上说同样的事情(第23.2.2.3节)

A more up-to-date draft says essentially the same thing (section 23.2.2.3)

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

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