下列情况下 Iterators 什么时候失效

When do Iterators in the following cases become invalidated

假设我有一个 std 容器,里面总共有一百个元素。 我再从container.My题中去掉第50个元素如下

1-If the container is a `std::vector` what iterators will become invalidated.
2-If the container is a `std::list` what iterators will become invalidated.
3-If the container is a `std::deque` what iterators will become invalidated.
4-If the container is a `std::map` what iterators will become invalidated. 

以上是我的理解,如有错误请指正

1-如果向量中的第 50 个元素被删除,所有接下来的元素将向上移动一个步骤,因为向量是一个动态数组并且它是连续的。因此第 50 个索引之前的迭代器将有效,大于或等于 50 的迭代器将在 remove

之后失效

2-如果容器是一个列表(双 link 列表)并且第 50 个索引被删除,只有第 50 个索引之后或等于第 50 个索引的迭代器会受到影响。

3-如果容器是双端队列,我不确定哪个迭代器会失效

4-如果容器是映射,我相信所有的迭代器都会失效。

(0-100) 因为需要 reordering/sorting。如有错误请指正

1-If the 50th element is removed in a vector all the next elements will move one step up since a vector is a dynamic array and its contiguous. So iterators before 50th index would be valid and iterators greater than or equal to 50 would be invalidated after the remove

正确。 (Source)

2-If the container is a list (double link list) and 50th index is removed only the iterators after or equal to the 50th index would be affected.

不正确。只有指向第 50 个索引的迭代器无效(列表节点重新链接,而不是移动)(Source)

3-If the container is a deque I am not sure which iterators would get invalidated

"All iterators and references are invalidated, unless the erased elements are at the end or the beginning of the container, in which case only the iterators and references to the erased elements are invalidated." (Source)

4-if the container is a map I believe all the iterators would get invalidated.

不正确。与 std::list 相同的解释适用(std::map 是一棵链接树)。 (Source)