CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Location
    England
    Posts
    72

    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.

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    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 ...

  3. #3
    Join Date
    May 2006
    Location
    England
    Posts
    72

    Re: Iterating part way through a std::map

    Quote Originally Posted by superbonzo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured