Ginsutime

Cherno Map Basics

Mar 30th, 2022
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <map>
  2. #include <unordered_map>
  3. #include <iostream>
  4.  
  5. struct CityRecord
  6. {
  7.     std::string Name;
  8.     uint64_t Population;
  9.     double Latitude, Longitude;
  10. };
  11.  
  12. int main()
  13. {
  14.     std::vector<CityRecord> cities;
  15.  
  16.     cities.emplace_back("Melbourne", 5000000, 2.4, 9.4);
  17.     cities.emplace_back("Lol-town", 5000000, 2.4, 9.4);
  18.     cities.emplace_back("Paris", 5000000, 2.4, 9.4);
  19.     cities.emplace_back("Berlin", 5000000, 2.4, 9.4);
  20.     cities.emplace_back("London", 5000000, 2.4, 9.4);
  21.  
  22.     // searching for each city is a pain since you go through whole vector
  23.     for (const auto& city : cities)
  24.     {
  25.         if (city.Name == "Berlin")
  26.         {
  27.             city.Population;
  28.             break;
  29.         }
  30.     }
  31.  
  32.     std::map<std::string, CityRecord> cityMap;
  33.     cityMap["Melbourne"] = CityRecord{"Melbourne", 5000000, 2.4, 9.4};
  34.     cityMap["Lol-town"] = CityRecord{ "Lol-town", 5000000, 2.4, 9.4 };
  35.     cityMap["Paris"] = CityRecord{ "Paris", 5000000, 2.4, 9.4 };
  36.     cityMap["Berlin"] = CityRecord{ "Berlin", 5000000, 2.4, 9.4 };
  37.     cityMap["London"] = CityRecord{ "London", 5000000, 2.4, 9.4 };
  38.  
  39.     CityRecord& berlinData = cityMap["Berlin"];
  40.     berlinData.Population;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment