What is the lifetime and validity of C++ iterators?

人走茶凉 提交于 2019-11-28 23:37:43

In list all iterators remain valid during inserting and only iterators to erased elements get invalid during erasing.

In your case keeping iterator should be fine even when other elements deleted ahead and after the inserted Thing*.

EDIT:

Additional details for vector and deque:

Vector:

  • inserting --- All iterators get invalid if reallocation happens, otherwise its valid.
  • erasing ---- All iterators after erase point get invalid.

deque:

  • inserting --- All iterators get invalid.
  • erasing ---- All iterators get invalid.

This depends on the container you use.

Check: http://www.sgi.com/tech/stl/
Look at each containers documentation at the end their will be a description on the conditions that iterators stay valid under.

For std::list<> they remain valid under all conditions until the element they actually refer to is removed from the container (at this point they are invalid).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!