CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2010
    Posts
    2

    Multimap Key Issue

    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!

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Multimap Key Issue

    It sounds like your '<' isn't working quite right.

    Viggy

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Multimap Key Issue

    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

  4. #4
    Join Date
    Jun 2010
    Posts
    2

    Re: Multimap Key Issue

    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?

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Multimap Key Issue

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured