#include #include #include //hash table #include //balanced binary search tree using namespace std; void showStats(unordered_map M) { cout << endl; cout << "Size: " << M.size() << endl; cout << "bucket_count: " << M.bucket_count() << endl; cout << "load_factor: " << M.load_factor() << endl; cout << "max_load_factor: " << M.max_load_factor() << endl; cout << endl; } int main() { //map over key-value pairs of types (string, double) //key MUST be a hashable type //value can be anything unordered_map gpa; showStats(gpa); pair p1; p1.first = "Chris"; p1.second = 4.0; pair p2; p2.first = "Edgar"; p2.second = 2.8; pair p3; p3.first = "David"; p3.second = 1.0; //Let's insert these into the hash table //w.c. O(n) because of duplicate detection, a.c. O(1) gpa.insert(p1); gpa.insert(p2); gpa.insert(p3); showStats(gpa); //Can use bracket notation for ease of use gpa["Bob"] = 3.6; gpa["Schweller"] = 5.0; showStats(gpa); gpa["Robert"] = 1.25; gpa["Frank"] = 3.5; gpa["Kayla"] = 2.27; //Iterate through all items in the table for (auto x : gpa) { cout << x.first << ": " << x.second << " at bucket " << gpa.bucket(x.first) << endl; } gpa["Zelda"] = 4.0; //Induce a table resize when load_factor exceeds 1 showStats(gpa); //Can swap values if you want swap(gpa["Schweller"], gpa["Robert"]); //You can search/find items by key //w.c. O(n), a.c. O(1) //using "at" or bracket notation cout << "Edgar's gpa is: " << gpa.at("Edgar") << endl; cout << "Frank's gpa is: " << gpa["Frank"] << endl; //Iterate through all items in the table for (auto x : gpa) { cout << x.first << ": " << x.second << " at bucket " << gpa.bucket(x.first) << endl; } showStats(gpa); return 0; }