When do Iterators in the following cases become invalidated
This question already has an answer here:
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
Correct. (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.
Incorrect. Only iterators pointing at the 50th index are invalidated (the list nodes are relinked, not moved) (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.
Incorrect. The same explanation as for std::list
applies ( std::map
is a linked tree). (Source)
Your question has many duplicates, and a simple search would have revealed all your answers:
Also: See here
链接地址: http://www.djcxy.com/p/73348.html下一篇: 在下列情况下,迭代器何时失效