Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Tạo unordered_map của class tự định nghĩa trong C++


Trong hướng dẫn này, ta sẽ thảo luận chương trình để hiểu cách tạo unordered_map của class tự định nghĩa trong C++.

Để tạo unordered_map từ class tự định nghĩa, ta truyền hàm băm (hash function) làm đối số thứ ba.

Ví dụ

#include <bits/stdc++.h>
using namespace std;

struct Person {
    string first, last;
    Person(string f, string l) {
        first = f;
        last = l;
    }
    bool operator==(const Person& p) const {
        return first == p.first && last == p.last;
    }
};

class MyHashFunction {
public:
    size_t operator()(const Person& p) const {
        return p.first.length() + p.last.length();
    }
};

int main() {
    unordered_map<Person, int, MyHashFunction> um;
    Person p1("kartik", "kapoor");
    Person p2("Ram", "Singh");
    Person p3("Laxman", "Prasad");
    um[p1] = 100;
    um[p2] = 200;
    um[p3] = 100;
    for(auto e : um) {
        cout << "[" << e.first.first << ", " << e.first.last << "] => " << e.second << '\n';
    }
    return 0;
}

Kết quả

[Laxman, Prasad] => 100
[kartik, kapoor] => 100
[Ram, Singh] => 200