// Native_STLMap.cpp : Defines the entry point for the console application. /* _____________________________________________________________________________ S T L : M A P C O N T A I N E R C L A S S Ron Kessler Created 1/27/2017 Native Code Version Run with CTL-F5 A map is a container class in the STL that implements a binary tree to store its data. The map uses Key-Value pairs and the keys are always sorted. They are easy to work with because when you code them, they are just like an array. It is easy to add, update, and delete key-value pairs too. This example creates a simple grade book where I can keep track of student grades. It shows how to create, read, update, & delete data (CRUD). In .Net, this is called a SortedList! Better name. Built-in methods: size() size of the map (# of entries) clear() clears all entries erase() erase an entry find() can be used like this: if(grade_list.find("Tim") == grade_list.end()) { cout<<"Tim is not in the map!"< #include #include using namespace std; void NotifyUser (string); //function templates void ShowStudents (); string crlf = "\n"; string crlf2 = "\n\n"; //---create the structure of our map // A string for the student's name and a char for the grade map myGrades; int main() { NotifyUser ("Learning about Maps"); NotifyUser ("I just added some students to the map container...." + crlf2); NotifyUser ("Press any key to see the list....." + crlf2); cin.get (); //---add some students myGrades ["Ruth Anderson"] = 'B'; myGrades ["Alice Ho"] = 'A'; myGrades ["Randy Jones"] = 'C'; myGrades ["Lupe Rodriguez"] = 'A'; myGrades ["Nguyen Li"] = 'B'; myGrades ["Gus Cooper"] = 'D'; ShowStudents (); NotifyUser ("Press any key to change Randy Jone's grade....." + crlf2); cin.get (); //---change Randy's grade myGrades ["Randy Jones"] = 'B'; ShowStudents (); NotifyUser ("Press any key to delete Randy....." + crlf); cin.get (); //---remove Randy from the map myGrades.erase ("Randy Jones"); NotifyUser ("Press any key to see the updated list....." + crlf2); ShowStudents (); return 0; } //H E L P E R F U N C T I O N S void NotifyUser (string msg) { cout << msg << crlf; } void ShowStudents () { system ("cls"); //---cycle through the whole map.Auto tells the compiler to determine the data types for (auto person : myGrades) //a ranged-based for loop like a for-each loop in .Net { cout << person.first << "---->> " << person.second << crlf; } cout << crlf << "The class size is " << myGrades.size () << crlf2; return; }