gpt4 book ai didi

c++ - 对双端队列中的迭代器失效感到困惑

转载 作者:IT老高 更新时间:2023-10-28 22:35:12 26 4
gpt4 key购买 nike

我对双端队列中的迭代器失效感到有点困惑。(在 this 问题的上下文中)

以下是摘自 -- C++ 标准库:教程和引用,尼古拉 M. Josuttis

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 的摘录网站:

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.

恕我直言,双端队列是 block 的集合,第一个 block 沿一个方向增长,最后一个 block 沿相反方向增长。

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

push_back, push_front 不应该对双端队列迭代器产生任何影响(我同意 Josuttis)。

正确的解释是什么?标准对此有何规定?

最佳答案

来自 standard working draft

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

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 本身的迭代器

编辑:A more up-to-date draft说的基本相同(第 23.2.2.3 节)

关于c++ - 对双端队列中的迭代器失效感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/913980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com