纤维Fibre 2023-02-22 18:03 采纳率: 63%
浏览 33
已结题

这个unordered_map错在哪里呢?

#第一次尝试哈希结构,写了一个代码,立刻就错了!
代码如下:

#include <iostream> 
#include <unordered_map> 

// Data Structure to store a Student Information
struct Student 
{ 
    int id; 
    const char* name; 
    Student(int Id, const char* Name) : id(Id), name(Name) {}
}; 

// Function to print the student details 
void print_student(Student s) 
{ 
    std::cout << "ID: " << s.id << ", Name: " << s.name << std::endl; 
} 

// Driver Code 
int main() 
{ 
    // Creating a unordered map with Student structure 
    // as key and marks as value 
    std::unordered_map<Student, int> studentMarksMap; 

    // Inserting values in studentMarksMap 
    studentMarksMap.insert({ { 1, "Martin" }, 84}); 
    studentMarksMap.insert({ { 2, "Andrew" }, 87}); 
    studentMarksMap.insert({ { 3, "John" }, 98}); 

    // Traversing an unordered map 
    for (auto itr = studentMarksMap.begin(); itr != studentMarksMap.end(); itr++) 
    { 
        print_student(itr->first); 
        std::cout << "\tMarks: " << itr->second << std::endl; 
    } 

    return 0; 
} 

报错信息:

img

  • 写回答

2条回答 默认 最新

  • 关注

    如楼上所说, C++无序容器对于非基本类型需要定义hash函数以及operator==, 补充一个方法, 对于不想用仿函数的可用lambda方法

    #include <iostream>
    #include <unordered_map>
    
    // Data Structure to store a Student Information
    struct Student
    {
        Student(int Id, const char *Name)
            : id(Id)
            , name(Name)
        {}
    
        // Function to print the student details
        void print_student() const
        {
            std::cout << "ID: " << id << ", Name: " << name << std::endl;
        }
    
        auto operator==(const Student &rhs) const -> bool
        {
            if (this != &rhs)
            {
                return id == rhs.id && (strcmp(name, rhs.name) == 0);
            }
            return true;
        }
    
        [[nodiscard]] auto getid() const -> int
        {
            return id;
        }
    
        [[nodiscard]] auto getname() const -> const char *
        {
            return name;
        }
    
      private:
        int id = 0;
        const char *name = nullptr;
        friend std::hash<Student>;
    };
    
    // namespace std
    //{
    // template <>
    // struct hash<Student>
    //{
    //     auto operator()(const Student &stu) const -> size_t
    //     {
    //         return std::hash<int>()(stu.id) ^
    //                std::hash<std::string>()(std::string(stu.name));
    //     }
    // };
    // } // namespace std
    
    // Driver Code
    int main()
    {
        auto hash = [](Student stu) -> size_t {
            return std::hash<int>()(stu.getid()) ^
                   std::hash<std::string>()(std::string(stu.getname()));
        };
        // Creating a unordered map with Student structure
        // as key and marks as value
        std::unordered_map<Student, int, decltype(hash)> studentMarksMap(10, hash);
    
        // Inserting values in studentMarksMap
        studentMarksMap.insert({{1, "Martin"}, 84});
        studentMarksMap.insert({{2, "Andrew"}, 87});
        studentMarksMap.insert({{3, "John"}, 98});
    
        // Traversing an unordered map
        for (auto &itr : studentMarksMap)
        {
            itr.first.print_student();
            std::cout << "\tMarks: " << itr.second << std::endl;
        }
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月10日
  • 已采纳回答 3月2日
  • 创建了问题 2月22日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!