473,396 Members | 1,758 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,396 software developers and data experts.

multimap erase

sks
I have a question regarding std::multimap/iterators.

At the SGI website, it says "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 design/implemented a observer pattern that is priority based. It so
happened that observers were deregistering themselves while they were being
notified. In other words, we were iterating through the multipmap and the
iterator/observer we are currently on was being erased. I fixed this by
keeping a data member iterator (m_Next) that pointed to one past the current
observer. [there is more to my fix than this but the other stuff doesn't
really matter right now]

Suppose we had: { a, b, c, d, e }
I needed to use a reverse iterator to go from e to one past a for
notification.

Now, in my deregister, suppose we were notifying about c (m_Next would be
'b') and if c wanted to deregister itself, we would erase c without
invalidating m_Next.

This worked fine for the 2 observers that deregistered, however, while I was
notifying, I notified the same observer twice. It seemed to me that somehow
the rbtree readjusted and my iterator was now pointing to perhaps the "same"
node in the tree but that node had a different element in it now - is that
possible for a multimap? I think not given that quote I got from the SGI
website. Any thoughts here?

Here is the actual example:
Suppose we had: { a, b, c, d, e }
Observer d and b deregistered themselves while being notified. c is the
observer that was notified twice.
By the way, sorry about not posting any code - my class is quite complex and
I would have to rip apart a lot of the details (and compile) and post on
here.

By the way, I am doing my erase by doing this:
container.erase((+ri).base()); ri is the reverse_iterator. Even thought I am
doing this funky thing, the erase works fine.

I just want to know if any of you seen a multimap (or map) iterating over an
element/node multiple times. By the way, the element was only in there once.
I know this b/c I printed out the contents of the container right before
doing the notification.

Any thoughts? Thanks in advance.
Feb 22 '07 #1
4 4207
sks wrote:
I have a question regarding std::multimap/iterators.

At the SGI website, it says "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 design/implemented a observer pattern that is priority based. It so
happened that observers were deregistering themselves while they were being
notified. In other words, we were iterating through the multipmap and the
iterator/observer we are currently on was being erased. I fixed this by
keeping a data member iterator (m_Next) that pointed to one past the current
observer. [there is more to my fix than this but the other stuff doesn't
really matter right now]

Suppose we had: { a, b, c, d, e }
I needed to use a reverse iterator to go from e to one past a for
notification.

Now, in my deregister, suppose we were notifying about c (m_Next would be
'b') and if c wanted to deregister itself, we would erase c without
invalidating m_Next.

This worked fine for the 2 observers that deregistered, however, while I was
notifying, I notified the same observer twice. It seemed to me that somehow
the rbtree readjusted and my iterator was now pointing to perhaps the "same"
node in the tree but that node had a different element in it now - is that
possible for a multimap? I think not given that quote I got from the SGI
website. Any thoughts here?

Here is the actual example:
Suppose we had: { a, b, c, d, e }
Observer d and b deregistered themselves while being notified. c is the
observer that was notified twice.
By the way, sorry about not posting any code - my class is quite complex and
I would have to rip apart a lot of the details (and compile) and post on
here.

By the way, I am doing my erase by doing this:
container.erase((+ri).base()); ri is the reverse_iterator. Even thought I am
doing this funky thing, the erase works fine.

I just want to know if any of you seen a multimap (or map) iterating over an
element/node multiple times. By the way, the element was only in there once.
I know this b/c I printed out the contents of the container right before
doing the notification.

Any thoughts? Thanks in advance.

Which form of erase are you using? If you are using a reverse iterator
and calling base(), then you probably aren't erasing the element that
you think you are. Reverse iterators and iterators are related by the
following identity (see 24.4.1):
For an iterator i, &*(reverse_iterator(i)) == &*(i - 1)

So, if you have a reverse_iterator ri from a container c, the correct
way to erase the element it points to would be
c.erase(--ri.base()) ;

--
Alan Johnson
Feb 22 '07 #2
On Feb 22, 7:11 am, Alan Johnson <a...@yahoo.comwrote:
sks wrote:
I have a question regarding std::multimap/iterators.
At the SGI website, it says "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 design/implemented a observer pattern that is priority based. It so
happened that observers were deregistering themselves while they were being
notified. In other words, we were iterating through the multipmap and the
iterator/observer we are currently on was being erased. I fixed this by
keeping a data member iterator (m_Next) that pointed to one past the current
observer. [there is more to my fix than this but the other stuff doesn't
really matter right now]
Suppose we had: { a, b, c, d, e }
I needed to use a reverse iterator to go from e to one past a for
notification.
Now, in my deregister, suppose we were notifying about c (m_Next would be
'b') and if c wanted to deregister itself, we would erase c without
invalidating m_Next.
This worked fine for the 2 observers that deregistered, however, while I was
notifying, I notified the same observer twice. It seemed to me that somehow
the rbtree readjusted and my iterator was now pointing to perhaps the "same"
node in the tree but that node had a different element in it now - is that
possible for a multimap? I think not given that quote I got from the SGI
website. Any thoughts here?
Here is the actual example:
Suppose we had: { a, b, c, d, e }
Observer d and b deregistered themselves while being notified. c is the
observer that was notified twice.
By the way, sorry about not posting any code - my class is quite complex and
I would have to rip apart a lot of the details (and compile) and post on
here.
By the way, I am doing my erase by doing this:
container.erase((+ri).base()); ri is the reverse_iterator. Even thoughtI am
doing this funky thing, the erase works fine.
I just want to know if any of you seen a multimap (or map) iterating over an
element/node multiple times. By the way, the element was only in there once.
I know this b/c I printed out the contents of the container right before
doing the notification.
Any thoughts? Thanks in advance.

Which form of erase are you using? If you are using a reverse iterator
and calling base(), then you probably aren't erasing the element that
you think you are. Reverse iterators and iterators are related by the
following identity (see 24.4.1):
For an iterator i, &*(reverse_iterator(i)) == &*(i - 1)

So, if you have a reverse_iterator ri from a container c, the correct
way to erase the element it points to would be
c.erase(--ri.base()) ;
Speaking of reverse iterators, is there any special reason to use them
instead of normal iterators (like iterating from both ends on
different occasions)? If not you could use normal iterators and
construct the multimap with std::greater as comparator to get the same
effect.

--
Erik Wikström

Feb 22 '07 #3
sks
"Alan Johnson" <aw***@yahoo.comwrote in message
news:O4******************************@comcast.com. ..
>Which form of erase are you using? If you are using a reverse iterator
and calling base(), then you probably aren't erasing the element that you
think you are. Reverse iterators and iterators are related by the
following identity (see 24.4.1):
For an iterator i, &*(reverse_iterator(i)) == &*(i - 1)

So, if you have a reverse_iterator ri from a container c, the correct way
to erase the element it points to would be
c.erase(--ri.base()) ;
I am using the erase on the iterator - there isn't one for reverse_iterator.
In my implementation, I used "c.erase((++ri).base())" since that was what
Meyers recommended in his "Effective STL" book. I also assert that the
element I am pointing to by my reverse_iterator is the same as my iterator,
prior to the erase. The erase is definitely erasing the correct elements
since I check the container before and after.
Feb 22 '07 #4
sks

"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...

Speaking of reverse iterators, is there any special reason to use them
instead of normal iterators (like iterating from both ends on
different occasions)? If not you could use normal iterators and
construct the multimap with std::greater as comparator to get the same
effect.

========================

Good idea. I will try that today.

Nothing tells me that my usage of reverse_iterator is screwing up the
container (iteration) but then again, it's probably better to use iter.
rather than reverse iter.

Thanks.
Feb 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
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: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
3
by: Tony Young | last post by:
Hi, I have a multimap container. I want to eliminate all "duplicate" elements. By duplicate I mean something like (3, 4), (4, 3) and (4, 3), in which I want to eliminate any two of these...
4
by: Nick Keighley | last post by:
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...
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...
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...
20
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.