Modernes C++ programmieren

Okt 23, 2024

lst-0801-godb.cpp

//#(compile) c++; compiler:g132; options:-O3 -std=c++23; libs:-
// 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
 }