How to create an unordered_map of tuples in C++?
Tuple – A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed.
Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple then we have to explicitly provide it with a hash function that can hash a tuple.
unordered_map can take up to 5 arguments:
- Key: Type of key values.
- Value: Type of value to be stored against the key.
- Hash Function: A function that is used to hash the given key. If not provided it uses the default hash function.
- Pred: A function that is used so that no two keys can have the same hash values.
- Alloc: An object used to define the memory model for the map.
hash_function can be anything, given that it can hash the given key.
unordered_map<tuple<data_type, data_type, data_type>, data_type, hash_function> map_of_tuple;
Contents of the unordered_map: [700, 800, 900] ==> 1 [100, 200, 300] ==> 1 [400, 500, 600] ==> 0
Note: It is not required to pass a hash function in the map which is a self-balancing BST. In order words, map<tuple<data_type, data_type, data_type>, data_type> mp works fine.
Please Login to comment...