#include #include #include //hash table #include //balanced binary search tree using namespace std; void showStats(unordered_map < string, double> & M) { cout << "Number of items: " << M.size() << endl; cout << "Table size (num buckets): " << M.bucket_count() << endl; cout << "Load factor: " << M.load_factor() << endl; cout << "Max load factor: " << M.max_load_factor() << endl; } int main() { //Stores key-value pairs //key MUST be hashable //value can be anthing unordered_map gpa; //Map contain "pairs" pair s1; s1.first = "Bruno"; s1.second = 6.7; pair s2; s2.first = "Ernesto"; s2.second = 3.7; pair s3; s3.first = "Rapheal"; s3.second = 1.00; //take a took at hash table stats: showStats(gpa); //Insert key-value pairs: run time: // w.c O(n), because of dupliate checking gpa.insert(s1); gpa.insert(s2); gpa.insert(s3); //Bracket notation to insert gpa["Leo"] = 5.0; gpa["Tannin"] = 3.5; gpa["Victoria"] = 1.2; gpa["Schweller"] = 10.0; gpa["Wylie"] = 1.7; gpa["Joe"] = 4.0; //Search: .at() or [] runtime: w.c O(n), a.c. O(1) cout << "Bruno's gpa is: " << gpa.at("Bruno") << endl; cout << "Leo's gpa is: " << gpa["Leo"] << endl; cout << "Rapheal's gpa is: " << gpa["Rapheal"] << endl; //Can iterate through items cout << "All items in map: " << endl; for (auto x : gpa) { cout << x.first << ": " << x.second << endl; } //Let's look at stats after adding stuff cout << endl; cout << "table stats" << endl; showStats(gpa); return 0; }