473,218 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 4449
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kenneth Massey | last post by:
Is the following code guaranteed to work? I have a hashed set of pointers, which I would like to selectively delete from. Can I iterate through the set, deleting certain ones, without...
9
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the...
3
by: francois.chartrand | last post by:
Hi, I would like to know if there is a way to locate a specific pair in a multimap ? Example: std::multimap<int,int> myMap; std::multimap<int,int>::iterator myMapIterator;
8
by: Steve Edwards | last post by:
Hi, While iterating through a multimap, I need to replace elements that meet certain conditions with a new element. There doesm't seem to be a replace() function for multimaps, so I'm inserting...
2
by: srikpen | last post by:
I am unable to get the "is" I keep getting the below: the find : this the find : this what I want is: the find : this the find : is
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
1
by: Saile | last post by:
I want to give an array the values from the specific multimap's key's values. multimap<string,int> mymultimap; multimap<string,int>::iterator it;...
1
by: ambarish.mitra | last post by:
Hi all, I have a multimap, where key is an int and the value is a class. I can insert into the multimap, but finding it difficult to retrieve the value when keys match. I can do this with...
2
by: Nikhil.S.Ketkar | last post by:
Hi, How does the == operator for multimap in STL behave ? I was under the impression that this is supposed to properly compare multimaps for equality. It seems that what it actually does is just...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.