Click to See Complete Forum and Search --> : Multimap Key Issue


stormyweathers
June 22nd, 2010, 03:16 PM
Hi-

I've got a multimap problem. The key for the multimap is a class with a few data members. I've overloaded the < operator so that key look up should take all of the relevant data members into consideration.

But what I am observing is that the look up seems to not be working...i.e. it is returning keys with slightly different data members.

Also, when I call erase on the multimap and pass the key I want used to erase the values, it is deleting values that don't exactly match the key.

Is there some other operator I need to overload as well?

Thanks!

MrViggy
June 22nd, 2010, 04:24 PM
It sounds like your '<' isn't working quite right.

Viggy

MrViggy
June 22nd, 2010, 04:25 PM
Oh, wait, you say 'multi-map'. Are you sure you're deleting the correct entry? A multi-map allows duplicate keys, but does not guarantee what order they will be in (other then they will all be grouped together, when you're iterating over the entries).

Viggy

stormyweathers
June 22nd, 2010, 04:34 PM
I'm sure that I'm not deleting the correct entry. :)

I am iterating over the keys, but one of the keys should not have been returned from the equal range, so its value is getting erased as well.

So is it normally just the < operator then that I have to change?

MrViggy
June 22nd, 2010, 04:46 PM
Yes, the std template library only uses '<' for map and set comparisons. If that's the case, then your '<' isn't correct. It must be antisymmetric (if x < y is true, then y < x must be false); transitive (if x < y and y < z, then x < z must be true); and it must be irreflexive (x < x is always false).

One of these conditions must not be being met.

Viggy