Modernes C++ programmieren

Okt 23, 2024

lst-0801-book.cpp

// https://godbolt.org/z/Ka3czrn54 
#include <unordered_map>
#include <iostream>
#include <string>
using std::string; using std::unordered_map; using std::cout;
struct Stadt {
    string name_;
    explicit Stadt(const string &name) : name_{name} {}
    auto operator<=>(const Stadt &b) const = default;
 };
 struct StadtHash {
     std::hash<string> sHash;
     size_t operator()(const Stadt& a) const {
         return sHash(a.name_);
     }
 };
 int main() {
     unordered_map<Stadt,string,StadtHash> abk{
         {Stadt{"Bielefeld"}, "BI"},
         {Stadt{"Jetzendorf"}, "JE"},
         {Stadt{"Tharandt"}, "TH"},
         };
     cout << abk[Stadt{"Bielefeld"}] << '\n'; // Ausgabe: BI
 }