月迷津渡` 2023-03-31 11:37 采纳率: 70%
浏览 26

关于#c++#的问题:假如map类m的key值是一个Student类,现在已经重载比较运算符了,如果给出name或者id或者age,该怎么根据给出的关键字来在m中进行哈希查找

#include <iostream>
#include <string>
#include <map>
using namespace std;
class Student
{
public:
    string name;
    string id;
    int age;
    bool operator()(const Student& s1, const Student& s2)
    {
        if (s1.name == s2.name)
        {
            if (s1.id == s2.id)
            {
                return s1.age > s2.age;
            }
            else
            {
                return s1.id > s2.id;
            }
        }
        else
        {
            return s1.name > s2.name;
        }
    }
};
int main()
{
    map<Student, int>m;
    return 0;
}

假如map类m的key值是一个Student类,现在已经重载比较运算符了,如果给出name或者id或者age,该怎么根据给出的关键字来在m中进行哈希查找?

  • 写回答

3条回答 默认 最新

  • threenewbee 2023-03-31 11:43
    关注
    #include <iostream>
    #include <string>
    #include <map>
    #include <algorithm>
    using namespace std;
    class Student
    {
    public:
        string name;
        string id;
        int age;
        bool operator()(const Student& s1, const Student& s2)
        {
            if (s1.name == s2.name)
            {
                if (s1.id == s2.id)
                {
                    return s1.age > s2.age;
                }
                else
                {
                    return s1.id > s2.id;
                }
            }
            else
            {
                return s1.name > s2.name;
            }
        }
    };
    // 定义比较函数
    bool compareName(const pair<Student, int>& p, const string& name)
    {
        return p.first.name == name;
    }
    bool compareId(const pair<Student, int>& p, const string& id)
    {
        return p.first.id == id;
    }
    bool compareAge(const pair<Student, int>& p, const int& age)
    {
        return p.first.age == age;
    }
    int main()
    {
        map<Student, int>m;
        // 在map中添加元素
        Student s1 = { "Tom", "001", 20 };
        Student s2 = { "Jack", "002", 22 };
        Student s3 = { "Lucy", "003", 21 };
        m[s1] = 100;
        m[s2] = 90;
        m[s3] = 80;
    
        // 根据名字查找
        string name = "Jack";
        auto it1 = find_if(m.begin(), m.end(), [&](const pair<Student, int>& p) {return compareName(p, name); });
        if (it1 != m.end())
        {
            cout << "找到了,名字为" << it1->first.name << ",成绩为" << it1->second << endl;
        }
        else
        {
            cout << "未找到" << endl;
        }
    
        // 根据id查找
        string id = "002";
        auto it2 = find_if(m.begin(), m.end(), [&](const pair<Student, int>& p) {return compareId(p, id); });
        if (it2 != m.end())
        {
            cout << "找到了,id为" << it2->first.id << ",成绩为" << it2->second << endl;
        }
        else
        {
            cout << "未找到" << endl;
        }
    
        // 根据年龄查找
        int age = 21;
        auto it3 = find_if(m.begin(), m.end(), [&](const pair<Student, int>& p) {return compareAge(p, age); });
        if (it3 != m.end())
        {
            cout << "找到了,年龄为" << it3->first.age << ",成绩为" << it3->second << endl;
        }
        else
        {
            cout << "未找到" << endl;
        }
        return 0;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月31日

悬赏问题

  • ¥30 arduino vector defined in discarded section `.text' of wiring.c.o (symbol from plugin)
  • ¥20 关于#c++#的问题:(2)运算二叉树·表达式一般由一个运算符和两个操作数组成:(相关搜索:二叉树遍历)
  • ¥20 如何训练大模型在复杂因素组成的系统中求得最优解
  • ¥15 关于#r语言#的问题:在进行倾向性评分匹配时,使用“match it"包提示”错误于eval(family$initialize): y值必需满足0 <= y <= 1“请问在进行PSM时
  • ¥45 求17位带符号原码乘法器verilog代码
  • ¥20 PySide6扩展QLable实现Word一样的图片裁剪框
  • ¥15 matlab数据降噪处理,提高数据的可信度,确保峰值信号的不损失?
  • ¥15 怎么看我在bios每次修改的日志
  • ¥15 python+mysql图书管理系统
  • ¥15 Questasim Error: (vcom-13)