So I have a multimap, where the key is a struct, and the values are another struct.

multimap<struct, struct> multimap1;

multimap1 contains (format is key: value)

1: Random Value 1
2: Random Value 2
3: Random Value 3
4: Random Value 4

I am trying to erase a value and a key from the multimap. Let's say I want to remove the second entry of the multimap. The result should be this:

1: Random Value 1
3: Random Value 3
4: Random Value 4

I have an iterator that points the second value, so when I erase it using multimap1.erase(it), it should remove the second entry (at least I think it should). I have been reading up on removing entries and from multimaps and it seems that only the value is removed from the multimap, not the key. So if erase the second entry with an iterator pointing to the second value, it would do this (Correct me if I am wrong):

1: Random Value 1
2: 
3: Random Value 3
4: Random Value 4

Is there a way to get the middle value result?

Edit: So I am apparently wrong. If you do erase, you do get the middle result. I am trying to figure why I am not erasing correctly because, my code segfaults when I call the erase. If I call it->first, it returns the key of the element I want removed. If I call it->second, it returns the value of that key. So if I call multimap.erase(it), It should remove the entire thing, correct?

Edit 2: In my code, I printed out it->first and it->second to make sure that the pointer was pointing to the correct element and it was. When I called multimap.erase(it), I stopped the loop and printed out the remaining elements in the multimap. It prints the first element correctly, but then the second element is printed something like this (output from compiler):

GEWolfC?`6??2?x3??2??2?x 1@4?????BUYA????BadWolfCorp1????AMstyKrq?4?X3??(4??3?    
X3PlanetExpress1GEqp7?L???5?d?5?x5??5?d2q7?7?X3??(4??3?X3??

So obviously, it is not deleting correctly. I am running g++ 4.7 on linux.

Edit 3: Sorry for so many edits, I want to keep the original question so people can reference it in the future. Just to experiment, I called multimap.erase(it->first) and that worked correctly. Can someone explain to me why this worked and what is going on here?

有帮助吗?

解决方案

erase does remove the whole entry (the "key-value" pair), as you want.

Sometimes, confusion arises when talking about map types since the library specification defines value_type to mean the "key-value" pair, not just the "value" (which is defined as mapped_type); so the word "value" is used interchangabley to mean one or the other.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top