#include #include using namespace std; /*************************** * Define a Student class */ class Student { int mId; string mName; public: Student(int id, string name) : mId(id), mName(name) { } void displayInfo() const { cout << "Student ("<()(obj.getName()); //Note: hash is a template class defined in C++ STL in which the operator() is overloaded //Here we call operator() of hash class, passing obj.GetName() as parameter. // basically, this will use system provided hash function to hash a string into an unsigned int } }; // Custom comparator that compares the Student objects by name struct StudentComparatorByName { bool operator()(const Student & obj1, const Student & obj2) const { if (obj1.getName() == obj2.getName()) return true; return false; } }; //Below we define "function objects" which package functions as a class/struct type. struct StudentHasherByID { static int a; //here are configurable parameters // overload operator (), so that StudentHash can be called as a function size_t operator()(const Student & obj) const { return hash()(a*obj.getId()); //Note: hash is a template class defined in C++ STL in which the operator() is overloaded //Here we call operator() of hash class, passing a*obj.getID() as parameter. // basically, this will use system provided hash function to hash a int into an unsigned int } }; /********************************* * Beginning main/driver *************************/ int StudentHasherByID::a; //static member variable needs to be defined (to get space) // declare two different sets of student, using different hash function and comparasion function typedef unordered_set SetOfStudentName; //use StudentHasherByName to hash a Student object //Use StudentComparatorByName to test if two Student objects are duplicate typedef unordered_set SetOfStudentID; //Use == operator to test if two Student objects are equivalent (duplicates) // a function to be used to examine the hash table details of the set void DisplayHashTable(SetOfStudentName &); int main() { int id; string name; char op; cout <<"This is a demo program for unordered_set class\n"; /************************************ * Demonstrate usage of function object ***********************************/ cout <<"Enter value of a to be used by hasherByID (non-zero integer):"; cin >> StudentHasherByID::a; cout <<"Demo hashers\n"; Student st (14, "Alice"); st.displayInfo(); cout <<" is hashed to "< setOfStud; //Note: if we don't provide the third parameter, the == operator on Student class // will be used //We can query unordered_set to get the hash function being used SetOfStudentName::hasher fn = setOfStud.hash_function(); /************************************************ //Display the hash policy ***********************************************/ cout <<"When load factor reaches this value, the bucket size is increased and elements are rehashed.\n"; cout <<"current Max_load_factor is" << setOfStud.max_load_factor()<> op; if (op=='y') { cout <<"Enter new load factor (a positive float value: "; cin >> loadFactor; setOfStud.max_load_factor(loadFactor); cout <<"Max_load_factor is now " << setOfStud.max_load_factor()<> id>> name; Student st (id, name); cout <<" This student is hashed to "<> op; if (op=='n') break; } cout <<"Search for student with key (name)\n"; while (true){ cout <<"Enter a name:"; cin >> name; Student st3(0,name); //just fill in the key field (i.e., the field //used to hash the object SetOfStudentName::const_iterator got=setOfStud.find(st3); if (got==setOfStud.end()) cout <<"student "<displayInfo(); } cout <<"continue(y/n)?"; cin >> op; if (op=='n') break; } return 0; } void DisplayHashTable(SetOfStudentName & set) { //Display size (number of elements), number of buckets, load factor cout <<"\n---------------------------------------\n"; unsigned size = set.size(); cout << "There are " << size << " elements:\n"; unsigned nbuckets = set.bucket_count(); cout << "There are " << nbuckets << " buckets:\n"; cout <<"Current load factor (size/bucket_count):"<