Advertisement
Ginsutime

Cherno Map Unordered

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