-
December 17th, 2013, 05:53 AM
#1
Iterating part way through a std::map
I have a std::map which I want to be able to iterate part way though, then go off and do "other things" and later resume iteration where I left off.
There is a chance that items may be added or removed from the std::map during doing the "other things".
Is there a correct way to go about this?
Any suggestions welcome
Thanks,
AnotherMuggle
Last edited by AnotherMuggle; December 17th, 2013 at 06:00 AM.
-
December 17th, 2013, 06:17 AM
#2
Re: Iterating part way through a std::map
iterators remain valid after insertions/deletions ( excluding iterators pointing to deleted elements, of course ) so it's safe to "resume" iteration without causing undefined behavior. That said, it may still be unsafe from a logical point of view, because you're only guaranteed that elements are traversed in order and nothing more in this case ( you could end up missing some of them ). So, it depends on what your goal is at a higher level ...
-
December 17th, 2013, 06:23 AM
#3
Re: Iterating part way through a std::map
Originally Posted by superbonzo
iterators remain valid after insertions/deletions ( excluding iterators pointing to deleted elements, of course ) so it's safe to "resume" iteration without causing undefined behavior. That said, it may still be unsafe from a logical point of view, because you're only guaranteed that elements are traversed in order and nothing more in this case ( you could end up missing some of them ). So, it depends on what your goal is at a higher level ...
Thanks superbonzo, that helps!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|