• C++中自定义数据结构作为unordered-set以及set中的元素


    #include 
    #include 
    #include 
    #include 
    
    class Person {
    public:
        std::string name;
        int age;
    
        Person(std::string name, int age) : name(name), age(age) {}
    
        friend struct std::hash<Person>;
    
        size_t operator()(const Person &p) const {
            size_t hash_name = std::hash<std::string>{}(p.name);
            size_t hash_age = std::hash<int>{}(p.age);
            return hash_name ^ hash_age;
        }
        // 作为unordered_set中元素需要使用operator==重载以及hansh函数
        bool operator==(const Person &other) const {
            return name == other.name && age == other.age;
        }
        
        // 作为set中的元素需要使用operator<重载 注意不要忘了两个const
        bool operator<(const Person &other) const {
            if (name == other.name) {
                return age < other.age;
            }
            return name < other.name;
        }
    };
    
    
    // 注意要实现这个
    namespace std {
        template<>
        struct hash<Person> {
            size_t operator()(const Person &p) const {
                return p(p);
            }
        };
    }
    
    int main() {
        std::unordered_set<Person> people_unordered;
        people_unordered.insert(Person("Alice", 30));
        people_unordered.insert(Person("Bob", 25));
        people_unordered.insert(Person("Alice", 30));
    
        std::set<Person> people_ordered;
        people_ordered.insert(Person("Alice", 30));
        people_ordered.insert(Person("Bob", 25));
        people_ordered.insert(Person("Alice", 30));
    
    
        std::cout << "Unordered Set:" << std::endl;
        for (const auto &person: people_unordered) {
            std::cout << person.name << " - " << person.age << std::endl;
        }
    
    
        std::cout << "\nOrdered Set:" << std::endl;
        for (const auto &person: people_ordered) {
            std::cout << person.name << " - " << person.age << std::endl;
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    上述实现hash有一种写法,下面给出第二种写法

    #include 
    #include 
    #include 
    
    class MyData {
    private:
        int id;
        std::string name;
    
    public:
        MyData(int _id, const std::string &_name) : id(_id), name(_name) {}
    
        friend struct MyDataHash;
    
        bool operator==(const MyData &other) const {
            return (id == other.id) && (name == other.name);
        }
    
        friend std::ostream &operator<<(std::ostream &os, const MyData &data) {
            os << "ID: " << data.id << ", Name: " << data.name;
            return os;
        }
    };
    
    struct MyDataHash {
        size_t operator()(const MyData &data) const {
            return std::hash<int>()(data.id) ^ std::hash<std::string>()(data.name);
        }
    };
    
    int main() {
        std::unordered_set<MyData, MyDataHash> mySet;
        mySet.insert(MyData(1, "Alice"));
        mySet.insert(MyData(2, "Bob"));
        mySet.insert(MyData(1, "Alice"));
    
        for (const auto &data: mySet) {
            std::cout << data << std::endl;
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    使用vscode进行简单的多文件编译
    【前端设计】SDC中生成时钟create_generated_clock语法解析
    京东一小伙一年输出20篇专利,其实你也可以
    如何免费获取CDH集群技术支持
    反虚拟机、反沙箱技术整理汇总
    适配器模式和装饰器模式
    【医学影像处理】基于MRIcron的dcm2nii批量dcm转nii格式
    40 道基础Dubbo 面试题及答案
    生产者消费者模型
    每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
  • 原文地址:https://blog.csdn.net/xiaohuihui1994/article/details/136415583