Results 1 to 8 of 8

Thread: std::multimap

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    std::multimap

    I cannot seem to insert items without confusing errors I do not understand.

    I want a lists or ints stored by strings to test this:

    multimap<string, int> Map;
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: std::multimap

    PHP Code:
        Map.insert(pair<stringint>("a"5));
        
    Map.insert(pair<stringint>("a"6));
        
    Map.insert(pair<stringint>("a"7));
        
    Map.insert(pair<stringint>("b"8));
        
    Map.insert(pair<stringint>("b"9));
        
    Map.insert(pair<stringint>("b"0));
        
    std::multimap<stringint>::iterator itr Map.lower_bound("a");

        for(
    itritr != Map.upper_bound("a"); itr++)
        {
            
    std::cout << "value: " << itr->second << std::endl;
        } 
        for(
    itritr != Map.upper_bound("b"); itr++)
        {
            
    std::cout << "value: " << itr->second << std::endl;
        } 


    Do I have the idea?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  3. #3
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: std::multimap

    That looks almost right, only:
    - you don't want to call uper_bound every iteration of the loop, since it has to do a lookup in the multimap
    - itr could be invalid inside the second loop, of course it isn't because you just initialized the data, but if there were no "a"s, then itr == Map.end() != Map.upper_bound("b").
    - instead of calling an explicit std:air constructor you can also use the std::make_pair function, it deduces the template arguments

  4. #4

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: std::multimap

    Okay, those are all noted.

    Now, as for the comparisons, is there a quick way to have the multimap do comparisons based on keys.

    Lets say the key is "a" and I wanna know if 10 exists in the "a" key.

    Would I iterate through like above and just do a comparison?
    Or is there a function provided for that?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  5. #5

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: std::multimap

    As for the last post I made I am also having trouble removing one element from a key.

    map.erase("a"); -- erase's all elements in the "a" key
    map.erase(map.begin(), map.end()) -- _FIRST, _LAST -- erase's whole map

    now lets say my map has
    "a" -- 1,2,3,4,5,6,7,8,9,0

    I want to erase 5.
    map.erase(map.lower_bound("a"), _____);
    See I am not sure on the best way to remove the fifth element except to search through the keys and find it.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: std::multimap

    In both cases a loop from lower_bound to upper_bound is the best option. If this is something you will be doing a lot then perhaps multimap is not the right container. A multimap usually means that you don't really care about which value you get with a specific key, they are all equall.
    You could try std::map<string, std::set<int> > (not sure how well it would perform), or std::set<std:air<string, int> >

  7. #7

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: std::multimap

    It is something I need to do a lot.
    Each message put into a queue will come with a Key, then the key is searched for a the message, if the message exists then that message will transmit to the Key associated with the objects.


    So, I do not think anything else will be much faster, unless there is a function which simply goes check(dwKey, dwMsg) -- returns true if the msg is in the key.


    Also, again, how can I remove a single element from a key like in the post above?
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: std::multimap

    As far as I know you have to use a loop to remove a specific key/value combination:
    Code:
    typedef std::multimap<std::string, int> Messages;
    Messages messages;
    string to_remove_key = ...;
    int to_remove_value = ...;
    Messages::iterator it    = messages.lower_bound(to_remove_key);
    Messages::iterator end = messages.upper_bound(to_remove_key);
    while (it != end) {
        if (it->second == to_remove_value) {
            Messages::iterator to_remove = it;
            ++it; // iterator becomes invalid on erase
            messages.erase(to_remove);
        } else {
            ++it;
        }
    }

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