Unordered Map does not contain a hash function for a pair like it has for int, string, etc, So if we want to hash a pair then we have to explicitly provide it with a hash function that can hash a pair. unordered_map can takes upto 5 arguments:
- Key : Type of key values
- Value : Type of value to be stored against the key
- Hash Function : A function which is used to hash the given key. If not provided it uses default hash function.
- Pred : A function which is used so that no two keys can have 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.
Prerequisite : How to create an unordered_map of user defined class?
Contents of the unordered_map : [1000, 2000] ==> 1 [2005, 3005] ==> 1 [2000, 3000] ==> 0
Exercise Problem : Nikhil is a travelling salesman and today he is visiting houses in a new locality to sell encyclopedias. The new city is in the from of a grid of x*y(1<=x<=10^9, 1<=y<=10^9) and at every intersection there is a house. Now he is not very good with remembering the houses that he has already visited, so whenever he goes into a house he tells you the coordinate of the house. Your job is to remember the coordinate and at the end of the day tell him all the houses that he visited on that day.
Enter the number of houses that he visited today :5
Enter the coordinate of HouseNo. 1 :1000 12985
Enter the coordinate of HouseNo. 2 :12548 25621
Enter the coordinate of HouseNo. 3 :14586 26481
Enter the coordinate of HouseNo. 4 :12 63
Enter the coordinate of HouseNo. 5 :14689 36945
Houses that he visited today:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to create an array with key value pairs in PHP?
- How to create dictionary and add key–value pairs dynamically?
- Ways to form n/2 pairs such that difference of pairs is minimum
- Does C++ compiler create default constructor when we write our own?
- When does compiler create default and copy constructors in C++?
- How to create a dynamic 2D array inside a class in C++ ?
- OpenCV C++ Program to create a single colored blank image
- Array of Strings in C++ (5 Different Ways to Create)
- How to create an HTML button that acts like a link?
- How to create optional arguments for a SASS mixin?
- Different ways to create an Object in C#
- How to create and deploy your portfolio in under 10 minutes
- Create pandas dataframe from lists using zip
- Different ways to create Pandas Dataframe
- Create an unordered list without any bullets using CSS
- Python program to create Bankaccount class with deposit, withdraw function
- Python | Create a Pandas Dataframe from a dict of equal length lists
- How to create a TreeMap in reverse order in Java
- 10 Ways to Create a Stream in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.