For how long the iterator returned by std::set.find() lives?

南笙酒味 提交于 2019-12-08 17:33:50

问题


I need to keep track of std::set element by saving the iterator returned by set.find().

My questions is does insertion and removing other elements invalidates the obtained iterator? From a simple test I did I can see it is not, but I'd like to ensure this feature is by design.


回答1:


It never invalidates iterators or pointers/references to the elements. Only if you remove the element itself does the iterator or pointer/reference become invalid.

23.1.2/8:

The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.



来源:https://stackoverflow.com/questions/2364662/for-how-long-the-iterator-returned-by-stdset-find-lives

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