#include #include #include #include #include using namespace std; using hashUS = unordered_map; // prototypes hashUS::iterator getInteratorForName(hashUS&, const string& name); ostream& operator<<(ostream&, const hashUS&); unsigned rand100(); int main() { hashUS students; using US = pair; students[rand100()] = "John"; students.insert(US(rand100(),"Paul")); US george{rand100(),"George"}; students.insert(george); auto ringo_num = rand100(); US ringo{ringo_num,"Ringo"}; students.insert(move(ringo)); cout << students << endl; // What does this mean? students[50]; cout << students << endl; // Try to insert a new element using Ringo's number students[ringo_num] = "Ringo Clone"; cout << students << endl; // What is John's number? cout << "John's number is " << getInteratorForName(students,"John")->first << endl; auto it = getInteratorForName(students,"maybe"); if (it == students.end()) cout << "maybe ain't there" << endl; cout << "number of elements with key " << ringo_num << " = " << students.count(ringo_num) << endl; cout << "number of elements with key " << ringo_num+1 << " = " << students.count(ringo_num+1) << endl << endl; cout << "students.bucket_count()=" << students.bucket_count() << endl; } unsigned rand100() { return rand() % 100 + 1; } ostream& operator<<(ostream& out, const hashUS& obj) { out << left; for (auto it = obj.begin(); it != obj.end(); ++it) { out << setw(5) << it->first << setw(10) << it->second << endl; } return out; } hashUS::iterator getInteratorForName(hashUS& hash_us, const string& name) { for (auto it = hash_us.begin(); it != hash_us.end(); ++it) { if (it->second == name) return it; } return hash_us.end(); }