does multimap insert invalidate iterator?

Discussion in 'C++' started by Nick Keighley, Jan 23, 2006.

  1. Hi,

    I've checked out various documentation for multimap but can't
    find anywhere it explicitly stated that insert() invalidates multimap
    iterators.

    consider this pseudo code:-

    int DataItem::genDerived ()
    {
    DataItem::MultiMap::const_iterator iter =
    dataItems.lower_bound(parameterReference);
    DataItem::MultiMap::const_iterator end =
    dataItems.upper_bound(parameterReference);

    while (iter != end)
    {
    newItem = calcNewItem (iter);
    dataItems.insert (newItem);
    }
    }

    It selects a particular value in the mm and finds the lower and
    upper bounds on their location. It then loops on all of them
    calculating a derived value for each. This is inserted into the
    same mm. A bad idea?


    --
    Nick Keighley
     
    Nick Keighley, Jan 23, 2006
    #1
    1. Advertising

  2. Nick Keighley wrote:
    > Hi,
    >
    > I've checked out various documentation for multimap but can't
    > find anywhere it explicitly stated that insert() invalidates multimap
    > iterators.
    >
    > consider this pseudo code:-
    >
    > int DataItem::genDerived ()
    > {
    > DataItem::MultiMap::const_iterator iter =
    > dataItems.lower_bound(parameterReference);
    > DataItem::MultiMap::const_iterator end =
    > dataItems.upper_bound(parameterReference);
    >
    > while (iter != end)
    > {
    > newItem = calcNewItem (iter);
    > dataItems.insert (newItem);
    > }
    > }
    >
    > It selects a particular value in the mm and finds the lower and
    > upper bounds on their location. It then loops on all of them
    > calculating a derived value for each. This is inserted into the
    > same mm. A bad idea?


    From SGI:

    "Multimap has the important property that inserting a new element into a
    multimap does not invalidate iterators that point to existing elements.
    Erasing an element from a multimap also does not invalidate any
    iterators, except, of course, for iterators that actually point to the
    element that is being erased."

    I prefer not to suppose that, because if I change the container, I must
    change my code too, but you can assume iterators are not invalidated
    after an insert if you prefer.
     
    Carlos Martinez Garcia, Jan 23, 2006
    #2
    1. Advertising

  3. Nick Keighley

    Kai-Uwe Bux Guest

    Nick Keighley wrote:


    > I've checked out various documentation for multimap but can't
    > find anywhere it explicitly stated that insert() invalidates multimap
    > iterators.


    You did not find that statement because it it is false. The standard says
    about associative containers [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.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 23, 2006
    #3
  4. Kai-Uwe Bux wrote:
    > Nick Keighley wrote:
    >
    >
    > > I've checked out various documentation for multimap but can't
    > > find anywhere it explicitly stated that insert() invalidates multimap
    > > iterators.

    >
    > You did not find that statement because it it is false. The standard says
    > about associative containers [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.


    thanks.
    Ok so the iterator isn't invalid, but back to my pseudo code:-

    DataItem::MultiMap::const_iterator iter =
    dataItems.lower_bound(parameterReference);
    DataItem::MultiMap::const_iterator end =
    dataItems.upper_bound(parameterReference);

    while (iter != end)
    {
    newItem = calcNewItem (iter);
    dataItems.insert (newItem);
    iter++;
    }

    could you end up processing items more than once? There's
    something that seems to be doing that (there's a lot of code so isn't
    definitely the mm calls at fault).


    --
    Nick Keighley
     
    Nick Keighley, Jan 23, 2006
    #4
  5. Nick Keighley wrote:
    > Kai-Uwe Bux wrote:
    >
    >>Nick Keighley wrote:
    >>
    >>
    >>

    > Ok so the iterator isn't invalid, but back to my pseudo code:-
    >
    > DataItem::MultiMap::const_iterator iter =
    > dataItems.lower_bound(parameterReference);
    > DataItem::MultiMap::const_iterator end =
    > dataItems.upper_bound(parameterReference);
    >
    > while (iter != end)
    > {
    > newItem = calcNewItem (iter);
    > dataItems.insert (newItem);
    > iter++;
    > }
    >
    > could you end up processing items more than once? There's
    > something that seems to be doing that (there's a lot of code so isn't
    > definitely the mm calls at fault).


    I'm not familiar to multimap, but I suppose that depending on the
    position of the new element inserted, you could access it too, and
    calculate a new item from that item calculated previously.

    I suppose what you want to do is iterate through the original elements
    of the container, not through the original and the new elements, but
    only you know what you want to do.
     
    Carlos Martinez Garcia, Jan 23, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth Massey
    Replies:
    3
    Views:
    1,661
    Kenneth Massey
    Jul 21, 2004
  2. Steve Edwards
    Replies:
    8
    Views:
    1,127
  3. Replies:
    0
    Views:
    434
  4. Pallav singh
    Replies:
    2
    Views:
    441
    Pallav singh
    Mar 15, 2009
  5. K. Frank
    Replies:
    3
    Views:
    600
    K. Frank
    May 31, 2013
Loading...

Share This Page