Need a bit help with std::map and / or multimaps and algorithm
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

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

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

    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 12:45 PM.
    Rate my post if i it was useful!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

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

    Why not one of these?
    Code:
    std::map<int, std::vector<int> >
    std::map<int, std::list<int> >
    std::map<int, std::deque<int> >
    std::map<int, std::stack<int>  >
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2005
    Location
    Estonia
    Posts
    233

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

    Thank you for the reply.
    You gave me idea.

    I did function like this:

    Code:
    map<int, stack<int> > asi;
    
    int getLASTcmdindexfor(int commandid)
    {
    	int tmp=-1;
    	auto it= asi.find(commandid);
    	if(it==asi.end()) return tmp;
    		if(!asi[commandid].empty()){
    			tmp=asi[commandid].top();
    			asi[commandid].pop();	
    		}		
    	return tmp;
    }
    I pass him "commandid" and depending on how many specific "commandid" values in the map are, it returns me the INDEX for the latest one each time i call it, until there are no more of them in stack.
    And if there are no more specific "commandid"s it returns -1, indicating that there are no more such commands.

    I just tested this in simple console app and seems to work fine. I will put this now into my primary app and will see how it works there.

    Code:
    	asi[94].push(6);
    	asi[94].push(7);
    	
    	asi[95].push(8);
    	asi[95].push(9);
    	
    	
    
    	
    	cout << "Nr of lines for 94: " << asi[94].size() << endl;
    	
    	cout <<getLASTcmdindexfor(94) << endl;
    	cout <<getLASTcmdindexfor(94) << endl;
    	cout <<getLASTcmdindexfor(95) << endl;
    	cout <<getLASTcmdindexfor(95) << endl;

    Output:
    Nr of lines for 94: 2
    7
    6
    9
    8

    EDIT
    Made it a bit generic:
    Code:
    template <class Container>
    int getLASTcmdindexfor(Container &inmap, int commandid)
    {
    	int tmp=-1;
    	auto it= inmap.find(commandid);
    	if(it==inmap.end()) return tmp;
    		if(!inmap[commandid].empty()){
    			tmp=inmap[commandid].top();
    			inmap[commandid].pop();	
    		}		
    	return tmp;
    }
    Last edited by BytePtr; December 30th, 2013 at 03:11 PM.
    Rate my post if i it was useful!

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

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

    Quote Originally Posted by BytePtr View Post
    Is there any function for multimap or algorithm that returns me the latest specific "KEY" from multimap ?
    Have a look at lower_bound, upper_bound and equal_range.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center