/* ------------------------------------------------------------------
   String key is more usual for maps...
   ----------------------------------------------------------------- */

#include <iostream>
#include <map>

using namespace std;

int main ()
{
   // Define a map that stores:
   //
   //     (key:string, value:int)
   map<string,int> myMap;
  
   // Adding (key -> value) to map   
   myMap["john"]=1;
   myMap["mary"]=2;
   myMap["jake"]=3;
   myMap["anne"]=4;
  
   cout << "myMap[\"john\"] = " << myMap["john"] << endl;
   cout << "myMap[\"mary\"] = " << myMap["mary"] << endl;
   cout << "myMap[\"jake\"] = " << myMap["jake"] << endl;
   cout << "myMap[\"anne\"] = " << myMap["anne"] << endl;
   cout << endl;

   /* -------------------------------------------------------------
      How to iterate over a map WITHOUT knowing all the keys stored
      ------------------------------------------------------------- */
   cout << "\nHow to iterator over a map WITHOUT knowing all the keys:\n";
   map<string, int>::iterator it; // Define an iterator over this type of map
   for (it = myMap.begin(); it != myMap.end(); it++)
      cout << it->first << "," << it->second << endl;
   cout << endl;


   myMap["john"]=99;   // Updates associated value

   cout << "myMap[\"john\"] = " << myMap["john"] << endl;
   cout << "myMap[\"mary\"] = " << myMap["mary"] << endl;
   cout << "myMap[\"jake\"] = " << myMap["jake"] << endl;
   cout << "myMap[\"anne\"] = " << myMap["anne"] << endl;
   cout << endl;

   // test if a key is in the map
   cout << "myMap.count(\"john\") = " << myMap.count("john") << endl;
   cout << "myMap.count(\"mary\") = " << myMap.count("mary") << endl;
   cout << "myMap.count(\"x\") = " << myMap.count("x") << endl;

   return 0;
}
