CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    May 2005
    Location
    Estonia
    Posts
    235

    Question Need a bit help with std::map and / or multimaps and algorithm

    Hello.

    Im stuck with some problem(s).
    Currently i have a std::map like this:
    Code:
    map<int, int> mymap;
    In my procedure i add some items to this. When i have no duplicates for keys, all seems to work.
    Few examples of my keys and values (just examples) just to get the idea:
    Without duplicate keys:
    Code:
    mymap[257]=10;
    mymap[94]=25;
    mymap[98]=55;
    mymap[21]=11;

    When i need to work with duplicates (when one key can be used more than 1 time but different value)
    Code:
    mymap[257]=10;
    mymap[94]=25;
    mymap[98]=55;
    mymap[98]=70;
    mymap[21]=11;
    I know that for duplicate keys with different values i should use multimap.

    Lets say i switch to multimap..

    Now if you look the duplicates example above, you see that when i need to get value of 98 i get it with:
    Code:
    int i = mymap[98];
    // i is now: 55
    Whats even more important, sometimes i need the latest value of the specific key

    In this case:
    Code:
    int i = mymap[98];
    // i MUST be 70
    Is there any function for multimap or algorithm that returns me the latest specific "KEY" from multimap ?
    Or there some other containers that could help me?

    If its possible i would like to avoid multimap completely. I have seen that it differs from map pretty a lot and causes troubles in many areas.

    What about pairs?
    I can use C++11, boost and other latest technology if that matters.

    TIA for any tip or help.
    Last edited by BytePtr; December 30th, 2013 at 01:45 PM.
    Rate my post if i it was useful!

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