ikegami:

Good question. I'll have to check...

Update: It appears that you're correct--it seems that deleting a map element via the iterator would potentially shuffle the map enough that the iterator would be invalid. So to do the STL conversion, we'd need a bit of an algorithm change. (Like make a stack of iterators and push the ones you want to delete on the stack, and when the loop is done, pop off the items in the stack, deleting the things they point to.

Yecch. Probably a better way out there. But I don't know it...

Update 2: I've done some more digging. Deleting the element invalidates the iterator doing the pointing, but doesn't invalidate other iterators. So we could use a pair of iterators, one to scan and one to delete. A bit tricky looking, so I'll leave it as an exercise to the reader.... 8^)

I hope this is a permanent link!

Update 3 (and last!): I found a link in a mailing list that gives this handy list:

vector -- all vector iterators are invalidated on any change
string & wstring -- all string iterators are invalidated on any change
deque -- all deque iterators are invalidated on any change, except for beginning and end removals
list -- no list iterators are invalidated, except for those referring to a deleted element
set & multiset -- no iterators are invalidated except ones pointing to a removed element
map & multimap -- no iterators are invalidated except ones pointed to a removed element

--roboticus


In reply to Re^7: foreach to for (related to my last question) by roboticus
in thread foreach to for (related to my last question) by tricolaire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.