fork(1) download
  1. #include <map>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. int main()
  7. {
  8. //create a map
  9. std::map<std::string, unsigned int> mySuperCoolMap;
  10. mySuperCoolMap["Edward"] = 39;
  11. mySuperCoolMap["Daniel"] = 35;
  12. mySuperCoolMap["Carlos"] = 67;
  13. mySuperCoolMap["Bobby"] = 8;
  14. mySuperCoolMap["Alfred"] = 23;
  15.  
  16. std::cout << "\n\n";
  17.  
  18. //Ranged based for loop to display the names and age
  19. for (auto itr : mySuperCoolMap)
  20. {
  21. std::cout << itr.first << " is: " << itr.second << " years old.\n";
  22. }
  23.  
  24. //create another map
  25. std::map<std::string, unsigned int> myOtherSuperCoolMap;
  26. myOtherSuperCoolMap["Espana"] = 395;
  27. myOtherSuperCoolMap["Dominic"] = 1000;
  28. myOtherSuperCoolMap["Chalas"] = 167;
  29. myOtherSuperCoolMap["Brian"] = 238;
  30. myOtherSuperCoolMap["Angela"] = 2300;
  31.  
  32. //Display the names and age
  33. for (auto itr : myOtherSuperCoolMap)
  34. {
  35. std::cout << itr.first << " is: " << itr.second << " years old.\n";
  36. }
  37.  
  38. //create a vector of maps
  39. std::vector<std::map<std::string, unsigned int>> myVectorOfMaps;
  40.  
  41. myVectorOfMaps.push_back(mySuperCoolMap);
  42. myVectorOfMaps.push_back(myOtherSuperCoolMap);
  43.  
  44. std::cout << "\n\n";
  45.  
  46. //Display the values in the vector
  47. for (auto vecitr : myVectorOfMaps)
  48. {
  49. for (auto mapitr : vecitr)
  50. {
  51. std::cout << mapitr.first << " is: " << mapitr.second << " years old.\n";
  52. }
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout

Alfred is: 23 years old.
Bobby is: 8 years old.
Carlos is: 67 years old.
Daniel is: 35 years old.
Edward is: 39 years old.
Angela is: 2300 years old.
Brian is: 238 years old.
Chalas is: 167 years old.
Dominic is: 1000 years old.
Espana is: 395 years old.


Alfred is: 23 years old.
Bobby is: 8 years old.
Carlos is: 67 years old.
Daniel is: 35 years old.
Edward is: 39 years old.
Angela is: 2300 years old.
Brian is: 238 years old.
Chalas is: 167 years old.
Dominic is: 1000 years old.
Espana is: 395 years old.