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