Inserting into map while using iterator?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik Goldman

    Inserting into map while using iterator?

    Hello,

    I would like to know if it's legal to insert one or more items into a
    std::map while traversing it from begin() to end().

    E.g.

    for (it = mymap.begin(); it != mymap.end(); it++)
    {
    if (condition)
    mymap[key] = data;
    }

    Will the iterator get invalidated?

    Thanks.

    -- Henrik


  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: Inserting into map while using iterator?

    On 2007-10-21 15:23, Henrik Goldman wrote:
    Hello,
    >
    I would like to know if it's legal to insert one or more items into a
    std::map while traversing it from begin() to end().
    >
    E.g.
    >
    for (it = mymap.begin(); it != mymap.end(); it++)
    {
    if (condition)
    mymap[key] = data;
    }
    >
    Will the iterator get invalidated?

    No, inserting elements into a (multi)map/set does not invalidate
    iterators, also erasing elements only invalidates iterators to the
    deleted elements.

    --
    Erik Wikström

    Comment

    Working...