The unordered_set internally implements a hash table to store elements. By default we can store only pre definded type as int, string, float etc.
If we want to store the element of user defined type as structure then compiler will show an error because before storing elements into unordered_set compiler performs some checking. And while comparing two user defined type compiler can not compare them hence it generate an error.
So, in order to store a structure in a unordered_set, some comparison function need to be designed. Since unordered_set also store implements hash table to store elements we should also have to implement hash function to perform hashing related work.
Below method explains its implementation.
Implementation: We create a structure type and define a comparison function inside that structure that will used to compare two structure type objects. Since unordered_set internally implements hash function so we should have also implement the hash function for user defined type objects.
Syntax To store user defined type elements unordered_set should follow following syntax
unordered_set(elementType, MyHashType) us; // element type is user defined type and MyHashType is class implementing hash function
Below code explains it.
115 101 110 102
115 101 110 102
Below is another example where we use predefined hash functions to make overall hash function of our defined class.
[Laxman, Prasad] [kartik, kapoor] [Ram, Singh]
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- How to create an unordered_map of user defined class in C++?
- How to create a Java HashMap of user defined class type?
- How to create a user defined javap tool?
- How to create a Struct Instance Using a Struct Literal in Golang?
- User Defined Literals in C++
- 2D vector in C++ with user defined size
- User defined Data Types in C++
- How to implement user defined Shared Pointers in C++
- Function Interposition in C with an example of user defined malloc()
- Multi-set for user defined data type
- How to Create a New User in Windows 10?
- How to create a dynamic 2D array inside a class in C++ ?
- Program to create Custom Vector Class in C++
- How to create a CSS rule or class at runtime using jQuery ?
- How to create a custom String class in C++ with basic functionalities
- How to add a method to struct type in Golang?
- How to find the type of Struct in Golang?
- How to instantiate Struct using new keyword in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- How to print struct variables data in Golang?
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.