469,289 Members | 2,170 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

does multimap insert invalidate iterator?

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

Jan 23 '06 #1
4 4182
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
Jan 23 '06 #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.
Jan 23 '06 #3
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

Jan 23 '06 #4
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.
Jan 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kenneth Massey | last post: by
9 posts views Thread by Dennis Jones | last post: by
3 posts views Thread by francois.chartrand | last post: by
8 posts views Thread by Steve Edwards | last post: by
1 post views Thread by Saile | last post: by
1 post views Thread by ambarish.mitra | last post: by
2 posts views Thread by Nikhil.S.Ketkar | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.