Advertisement
Ginsutime

Cherno Map Ordered

Mar 30th, 2022
1,508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 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.     bool operator<(const CityRecord& other) const
  12.     {
  13.         return Population < other.Population;
  14.     }
  15. };
  16.  
  17. namespace std
  18. {
  19.     template<>
  20.     struct hash<CityRecord>
  21.     {
  22.         size_t operator()(const CityRecord& key)
  23.         {
  24.             // Have to construct and then call it, syntax is weird
  25.             // Cherno doesn't remember it, just ends up copy/pasting it from somewhere else
  26.             return hash<std::string>()(key.Name);
  27.         }
  28.     };
  29. }
  30.  
  31. int main()
  32. {
  33.     //std::unordered_map<CityRecord, uint32_t> foundedMap;
  34.  
  35.     //foundedMap[CityRecord{ "Melbourne", 5000000, 2.4, 9.4 }] = 1850;
  36.  
  37.     // Index operator [ will always insert things in, no const version of this operator
  38.     // If thing being inserted doesn't exist, it creates it and inserts it into unordered map and gives ref to new CityRecord
  39.     //uint32_t melBourneFounded = foundedMap[CityRecord{ "Melbourne", 3487382, 2.4, 9.4 }];
  40.  
  41.     std::map<std::string, CityRecord> cityMap;
  42.     cityMap["Melbourne"] = CityRecord{ "Melbourne", 5000000, 2.4, 9.4 };
  43.     cityMap["Lol-town"] = CityRecord{ "Lol-town", 5000000, 2.4, 9.4 };
  44.     cityMap["Paris"] = CityRecord{ "Paris", 5000000, 2.4, 9.4 };
  45.     cityMap["Berlin"] = CityRecord{ "Berlin", 5000000, 2.4, 9.4 };
  46.     cityMap["London"] = CityRecord{ "London", 5000000, 2.4, 9.4 };
  47.  
  48.     std::map<CityRecord, uint32_t> cityFounded;
  49.     // Can create new ones this way as well, since if it doesn't find it, it'll make it
  50.     // Sorted by pop count
  51.     cityFounded[CityRecord{ "Melbourne222", 5000000, 2.4, 9.4 }]; // No less than operator so we have to define it within custom class itself
  52.     cityFounded[CityRecord{ "Delbourne111", 70000000, 9.4, 2.4 }];
  53.     cityFounded[CityRecord{ "Melbourne000", 333000, 3, 5 }];
  54.  
  55.     // If you want to retrieve data without inserting it, use .at()
  56.     const auto& cities = cityMap;
  57.     //const CityRecord& berlinData = cities.at("Berlin");
  58.  
  59.     // How to check if data at .at is there
  60.     if (cities.find("Berlin") != cities.end())
  61.     {
  62.         const CityRecord& berlinData4 = cities.at("Berlin");
  63.     }
  64.  
  65.     // Below example but in C++14 AKA Stone Age
  66.     //for (auto& kv : cityMap)
  67.     //{
  68.     //  const std::string& name = kv.first;
  69.     //  CityRecord& city = kv.second;
  70.     //}
  71.  
  72.     // Iterate through entire data structure
  73.     // [ ] is structured bindings from C++17
  74.     for (auto& [name, city] : cityMap)
  75.     {
  76.         //std::cout << name << "\n Population: " << city.Population << std::endl;
  77.         cityFounded[city];
  78.     }
  79.  
  80.     for (auto& [city, founded] : cityFounded)
  81.     {
  82.         std::cout << city.Name << "\n Population: " << city.Population << std::endl;
  83.     }
  84.  
  85.     std::cin.get();
  86.  
  87.     // More efficient vs one below, doesn't copy as it creates it in place and references the memory
  88.     //CityRecord& berlinData2 = cityMap["Berlin"];
  89.     //berlinData2.Name = "Berlin";
  90.     //berlinData2.Population = 5;
  91.  
  92.     // Ends up copying from last line, not preferred to one above
  93.     //CityRecord berlinData3;
  94.     //berlinData3.Name = "Berlin";
  95.     //berlinData3.Population = 5;
  96.     //cityMap["Berlin"] = berlinData3;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement