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_iterato r. 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. 4 4226
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_iterato r. 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_iter ator(i)) == &*(i - 1)
So, if you have a reverse_iterato r ri from a container c, the correct
way to erase the element it points to would be
c.erase(--ri.base()) ;
--
Alan Johnson
On Feb 22, 7:11 am, Alan Johnson <a...@yahoo.com wrote:
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_iterato r. 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_iter ator(i)) == &*(i - 1)
So, if you have a reverse_iterato r 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
"Alan Johnson" <aw***@yahoo.co mwrote 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_iter ator(i)) == &*(i - 1)
So, if you have a reverse_iterato r 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_iterato r.
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_iterato r 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.
"Erik Wikström" <er****@student .chalmers.sewro te in message
news:11******** **************@ a75g2000cwd.goo glegroups.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_iterato r is screwing up the
container (iteration) but then again, it's probably better to use iter.
rather than reverse iter.
Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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 entire multimap, and if a
particular key has duplicates, only see one of them. I don't think I care
about which of the duplicate entries I see, because once I have an entry, I
can use lower_bound and upper_bound to iterate through them, right? ...
|
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,
|
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 three. The most
straightforward way I can think of is to first swap every member such
that every member has its key smaller than its value. Then loop thru
the multipmap and erase duplicates (please see the loop below). Is
there a more automatic way...
|
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 DataItem::genDerived ()
{
| |
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 a new element, and then deleting the old.
typedef multimap<double, MyStructType, greater<double> >;MyMultimap
MyMultimap map = ...
|
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;
pair<multimap<string,int>::iterator,multimap<string,int>::iterator> ret;
mymultimap.insert (pair<string,int>("test",10));
mymultimap.insert (pair<string,int>("test",20));
mymultimap.insert (pair<string,int>("ab",100));
mymultimap.insert (pair<string,int>("ab",200));
|
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 primitive types (int/char
etc).
Example:
class A {
|
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:
vector<stringvec;
multimap<stirng, intmyMap
// populate myMap
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |