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
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.
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
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.
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.
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