Hi,
New to using hash_tables and unordered_map. I was wondering if there was a way to handle inserting duplicate keys the same way unordered_map handles collisions. In other words to link/chain the duplicate key element under the original element in the container. And if there is, what is the easiest way to implement that in code.
You know the type of your Key and the type of your Value, but you want more than one value for each Key in the map. You want a collection of Values.
If you can't have std::unordered_map<T,U> due to multiple values
and can't have std::unordered_multimap<T,U> due to "because",
then there is std::unordered_map<T,C<U>> (where C is a suitable collection).
you are going to have to detect it yourself.
so when you get a key, use 'contains()' or 'at()' members to see if it is there without inserting it. If it isn't there, and you want to insert, ok. If it is there, and you want to insert, you can doctor the key and repeat until you find a key that has not been used. Appending a number is probably sufficient for classroom, eg if the key is 'joebob' then try joebob1, joebob2, ...
and, whatever you do here, you have to match it on your lookup so if you look something up and the key is there, you may want to find all the ones with doctored keys, so ...
another way is to make your map a map of containers, and add duplicates to the container when you get one. This is a little more complex. This sounds like what you want, so when you get a duplicate, just add it to the container under that key, don't mess with the map itself beyond fetching. This is as simple as if (.. .contains(key) ) mymap[key].data.push_back(value); assuming a map of vectors or whatnot that supports push back.