How does std::unordered_map find values? - c++

When iterating over an unordered map of values, std::unordered_map<Foo, Bar>, it will use an iterator pointing to values instd::pair<Foo, Bar>. This makes it seem like std::unordered_map stores its values internally as std::pairs of values.
If I'm not mistaken, std::unordered_map works by hashing the key and using that for lookups. So, if the internal structure is something like a list of pairs, how does it know which pair has the hashed key? Wouldn't it need to hash the whole pair, value included?
If the internal structure does not hold pairs, does calling std::unordered_map::begin() then create a std::pair object using the data in the map? Then how does modifying the data in the pair also modify the data in the actual map itself?
Thank you.

Let's pretend that the map is really a single vector:
std::vector<std::pair<Foo, Bar>> container;
It's easy to visualize that iterating over this container iterates over std::pairs of two classes.
The real unordered map works the same way, except instead of a vector the map stores std::pair<Foo, Bar>s in a hash table, with additional pointers that stitch the whole hash table together, that are not exposed via the map's iterators.
The way that associative containers, like maps, get loosely explained and described -- as a key/value lookup -- makes it sound like in the maps keys and values are stored separately: here's a hash table of keys, and each key points to its corresponding value. However that is not the case. The key and its value are kept together, tightly coupled in discrete std::pair objects, and the map's internal structure arranges them in a hashed table that the iterator knows how to iterate over.
So, if the internal structure is something like a list of pairs, how does it know which pair has the hashed key?
Neither. An unordered map can be loosely described as a:
std::vector<std::list<std::pair<Key, Value>>>
A key's hash is the index in the vector. All Keys in the ith position in this vector have the same hash value. All keys with the same hash are stored in a single linked list.

Related

How to retrieve elements using hash values in STL hash containers?

I am using unordered set to store strings. Here it is possible to have different strings to have same hash value, so I have to add to linked list for that hash value. Is this is supported in C++ stl unordered_set.
std::unordered_set<std::string, hashFunction> m_Dictionary;
I inserted "world: which has hash value 4 and another word "HellO" which has hash value 4 so both should go to the dictionary. How we can achieve this.
Another requirement is that I want to search by string, if string present in dictionary it should return true.
Also I want to search by hash value i.e., 4 then I should get output as "world" and "HellO".
Is this can be achieved by unordered_set or unordered_map. I want to use STL hash containers.
Basically I want to search using strings and with hash value. If hash value has multiple strings then we have to print all strings with that hash value. I am looking for sample code how we can achieve this.
I am aware that we can do this with out using STL hash containers, I am wondering is this possible with STL hash containers.
Thanks for help.
The hash container already supports objects that have the same hash. Basically, they are all pushed into a sublist and linearly searched through, so it is more expensive if your hash is poor. You can't search by hash value in the stl.
If you really need to use hash values then you will need to put a wrapper round an unordered_multimap indexed by integer, where your wrapper has hashed to the key, which will store multiple values at the same key. You can use equal_range() to return a pair of iterators covering the common key.

to store unsorted key value pair

Is there any container available in C++ STL to store unsorted key value pair with duplicate keys?
I was thinking std::unordered_multimap container will help me in this case but the elements with equivalent keys are grouped together in this.
I would recommend you to look at sequence containers. Basically you can store std::pair< key, value > at some sequence container.
If you just need to store key-value pairs and sometimes add new key-value pair at the end of the container then std::vector is enough. If you additionally want to insert elements in the beginning of the container then look at std::deque. And so on...
So the best strategy is to analyze your constraints and choose the appropriate sequence container.

Swapping each pair of values in a multimap<int, int>

I have a multimap containing over 5 million pairs and I need to swap the keys with the values.
unordered_multimap<int, int> edge;
Due to the large size of the container and the processes involved, I would prefer to not have to create a new multimap with the swapped pairs by iterating over each element of the map.
What would be the best way, if any, to do this in place?
The proper approach is not to do this at all, but instead to have a bi-directional map in the first place, on which you can perform lookup in either direction.
Consider looking into Boost.Bimap.
You can not do this in-place.
The map you have stored the elements based of the hash values of its key. If you want to hash on a different key (the former value) you have to rebuild the whole map or think of a different way to store the elements.
Boost.Bimap (as suggested by Lightness Races in Orbit) for example supports bidirectional unordered multimaps.

C++ std::hash_map: What is the key's role

Both maps and hash_maps are designed so hold pairs of <key, data>. It's clear to me why the map should have a key for it's sorting (more precisely: treeing), but I don't understand why hash_maps need a key, why can't it's data alone be hashed and placed into the hash table?
I couldn't find the answer neither in the documentation nor by searching around the net.
std::unordered_set works precisely in the way you describe. However, there are times when you want to map from one piece of data to another; that's where std::unordered_map comes into play.
Walk to the cupboard. Get the phone book out and look up a number. It has a mapping between a name and number
you are looking for set, where a key is also data.
C++ offers some different flavour of them: set, unordered_set, etc...
Hash Map which is also called Unordered Map uses a HASH of the KEY as an index of buckets or Slots.In other word, any Hash Table needs a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.These index are the Key of the Hash Table, which are used for accessing the data in O(1) time in best case.
If you want to use the data itself as the key, the appropriate container is std::set or std::unordered_set. A map holds both a key and a value; the difference between std::map and std::unordered_map is in how the data is organized; std::map sorts by the key, and std::unordered_map hashes by the key.

Why retrieving elements from CMap is not ordered

In my application, I have a CMap of CString values. After adding the elements in the Map, if I retrieve the elements in some other place, am not getting the elements in the order of insertion.Suppose I retrieve the third element, I get the fifth like that. Is it a behavior of CMap. Why this happens?
You asked for "why", so here goes:
A Map provides for an efficient way to retrieve values by key. It does this by using a clever datastructure that is faster for this than a list or an array would be (where you have to search through the whole list before you know if an element is in there or not). There are trade-offs, such as increased memory usage, and the inability to do some other things (such as knowing in which order things were inserted).
There are two common ways to implement this
a hashmap, which puts keys into buckets by hash value.
a treemap, which arranges keys into a binary tree, according to how they are sorted
You can iterate over maps, but it will be according to how they are stored internally, either in key order (treemap) or completely unpredictable (hashmap). Your CMap seems to be a hashmap.
Either way, insertion order is not preserved. If you want that, you need an extra datastructure (such as a list).
How about read documentation to CMap?
http://msdn.microsoft.com/ru-ru/library/s897094z%28v=vs.71%29.aspx
It's unordered map really. How you retrieve elements? By GetStartPosition and GetNextAssoc? http://msdn.microsoft.com/ru-ru/library/d82fyybt%28v=vs.71%29.aspx read Remark here
Remarks
The iteration sequence is not predictable; therefore, the "first element in the map" has no special significance.
CMap is a dictionary collection class that maps unique keys to values. Once you have inserted a key-value pair (element) into the map, you can efficiently retrieve or delete the pair using the key to access it. You can also iterate over all the elements in the map.