|
-
Jul 17th, 2005, 07:26 PM
#1
Thread Starter
PowerPoster
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
-
Jul 18th, 2005, 12:04 PM
#2
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.
-
Jul 18th, 2005, 01:39 PM
#3
Thread Starter
PowerPoster
Re: map vs multimap -- whats the difference?
Untrue, snip the following into a workspace:
PHP Code:
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;
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
-
Jul 18th, 2005, 01:49 PM
#4
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: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.
-
Jul 18th, 2005, 04:58 PM
#5
Thread Starter
PowerPoster
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
-
Jul 18th, 2005, 05:22 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|