does multimap insert invalidate iterator?

N

Nick Keighley

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?
 
C

Carlos Martinez Garcia

Nick said:
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.
 
K

Kai-Uwe Bux

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
 
N

Nick Keighley

Kai-Uwe Bux said:
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).
 
C

Carlos Martinez Garcia

Nick said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top