Sobes.tech
Middle+

Bir sınıfınızı map veya unordered_map içinde anahtar olarak kullanmak için ne yapılmalı?

sobes.tech yapay zeka

AI'dan gelen yanıt

std::map için:

Sınıfınız için karşılaştırma operatörü <'yi tanımlamanız gerekir.

#include <map>
#include <string>

struct MyKey {
    int id;
    std::string name;

    // "<" operatörünü tanımlıyoruz
    bool operator<(const MyKey& other) const {
        if (id != other.id) {
            return id < other.id;
        }
        return name < other.name;
    }
};

int main() {
    std::map<MyKey, int> myMap;
    MyKey key1{1, "apple"};
    MyKey key2{2, "banana"};
    myMap[key1] = 10;
    myMap[key2] = 20;
    return 0;
}

std::unordered_map için:

Kullanıcı tanımlı türleri hash'lemek için std::hash standart hash'ı kullanamaz, bu nedenle sınıfınız için bir hash fonksiyonu (hasher) tanımlamanız gerekir ve gerekirse == eşitlik operatörünü de tanımlamalısınız.

Hash fonksiyonunu tanımlama yolları:

  1. std isim alanında std::hash'ı aşırı yükleme (tavsiye edilmez):

    #include <unordered_map>
    #include <string>
    #include <functional> // std::hash için
    
    struct MyKey {
        int id;
        std::string name;
    
        // "==" operatörünü tanımlıyoruz
        bool operator==(const MyKey& other) const {
            return id == other.id && name == other.name;
        }
    };
    
    // MyKey için std::hash'in uzmanlaştırılması
    namespace std {
        template <>
        struct hash<MyKey> {
            size_t operator()(const MyKey& key) const {
                // Basitçe hash'leri birleştirme örneği
                // Daha karmaşık ve dayanıklı hash fonksiyonları tercih edilebilir
                return hash<int>()(key.id) ^ (hash<std::string>()(key.name) << 1);
            }
        };
    }
    
    int main() {
        std::unordered_map<MyKey, int> myUnorderedMap;
        MyKey key1{1, "apple"};
        MyKey key2{2, "banana"};
        myUnorderedMap[key1] = 10;
        myUnorderedMap[key2] = 20;
        return 0;
    }
    
  2. Hash fonksiyonunu şablon parametresi olarak geçmek:

    #include <unordered_map>
    #include <string>
    #include <functional>
    
    struct MyKey {
        int id;
        std::string name;
    
        // "==" operatörünü tanımlıyoruz
        bool operator==(const MyKey& other) const {
            return id == other.id && name == other.name;
        }
    };
    
    // Hash yapısının tanımı
    struct MyKeyHash {
        size_t operator()(const MyKey& key) const {
            return std::hash<int>()(key.id) ^ (std::hash<std::string>()(key.name) << 1);
        }
    };
    
    int main() {
        // MyKeyHash'i üçüncü şablon parametresi olarak geçiyoruz
        std::unordered_map<MyKey, int, MyKeyHash> myUnorderedMap;
        MyKey key1{1, "apple"};
        MyKey key2{2, "banana"};
        myUnorderedMap[key1] = 10;
        myUnorderedMap[key2] = 20;
        return 0;
    }
    

Her iki durumda da, == eşitlik operatörü de std::unordered_map için gereklidir.