#include #include #include #include #include using std::cout; using std::endl; using std::string; // Alias declarations using StudentId = unsigned; using Name = string; using Students = std::map; // function prototypes unsigned rand100u(); Students::const_iterator getInteratorForName(Students&, const Name& name); std::ostream& operator<<(std::ostream&, const Students&); int main() { Students students; // insert 4 Students into the map students[rand100u()] = "John Lennon"; students.insert(std::pair(rand100u(),"Paul McCartney")); using Student = std::pair; Student george{rand100u(),"George Harrison"}; students.insert(george); StudentId ringoId = rand100u(); Student ringo{ringoId,"Ringo Star"}; students.insert(std::move(ringo)); cout << students << endl; // What does this mean? students[50]; cout << students << endl; // Correct the spelling of Ringo's name students[ringoId] = "Ringo Starr"; cout << students << endl; // Remove Student 50 students.erase(students.find(50)); cout << students << endl; // What is John's number? cout << "John's number is " << getInteratorForName(students,"John Lennon")->first << endl << endl; auto it = getInteratorForName(students,"Mick Jagger"); if (it == students.end()) cout << "Mick Jagger ain't there" << endl << endl; // count cout << "number of elements with key " << ringoId << " = " << students.count(ringoId) << endl; cout << "number of elements with key " << ringoId+1 << " = " << students.count(ringoId+1) << endl; } unsigned rand100u() { return rand() % 100 + 1; } std::ostream& operator<<(std::ostream& out, const Students& students) { out << std::left; for (auto it = students.begin(); it != students.end(); ++it) { out << std::setw(5) << it->first << std::setw(10) << it->second << endl; } return out; } Students::const_iterator getInteratorForName(Students& Students, const string& name) { for (auto it = Students.cbegin(); it != Students.cend(); ++it) { if (it->second == name) return it; } return Students.end(); }