Results 1 to 6 of 6

Thread: map vs multimap -- whats the difference?

  1. #1

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

    map vs multimap -- whats the difference?

    Okay I though multimap was to map multiple values to one key and map was used to map 1 key to 1 value.


    It seems that is not true.
    Infact as I've been playing with map and multimap it seems you can insert multiple values for 1 key in both of them...

    cppreference.com shows that maps are for key/value pairs and that multimap allows duplicate keys...

    In both map and multimaps if I insert(1,2) then insert(1,3) i can get access to both 2,3 via 1
    "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
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: map vs multimap -- whats the difference?

    That's impossible. A map will overwrite the existing value if you insert another with the same key.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

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

    Re: map vs multimap -- whats the difference?

    Untrue, snip the following into a workspace:

    PHP Code:
    map<intintMap;
    Map.insert(pair<intint>(1,2));
    Map.insert(pair<intint>(1,3));

    map<intint>::iterator itr Map.lower_bound(1);
    cout << itr->second << endl;
    cout << ++itr->second  << endl;

    system("PAUSE"); 
    The output is:
    2
    3
    Last edited by Halsafar; Jul 18th, 2005 at 01:43 PM.
    "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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: map vs multimap -- whats the difference?

    Non-portable random behaviour.
    Code:
    int main() {
    
            map<int, int> Map;
            Map.insert(pair<int, int>(1,2));
            Map.insert(pair<int, int>(1,3));
    
            map<int, int>::iterator itr = Map.lower_bound(1);
            cout << itr->second << endl;
            ++itr;
            if(itr == Map.end()) {
                    cout << "Thought so." << endl;
            } else {
                    cout << itr->second  << endl;
            }
    
    }
    Output:
    Code:
    2
    Thought so.
    Dereferencing the end() iterator yields undefined behaviour.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

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

    Re: map vs multimap -- whats the difference?

    Well okay.
    How come the first output is 2 then on my snippet?

    I figured key 1 == value 3 since it is the last to be inserted.
    "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
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: map vs multimap -- whats the difference?

    Because insert() doesn't overwrite, I was wrong about that detail. It simply doesn't insert if the key already exists.
    http://msdn.microsoft.com/library/de...fmapinsert.asp
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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