月迷津渡` 2023-03-31 14:46 采纳率: 70%
浏览 31

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

#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;
        }
    }
 
    // 新增函数,根据给出的关键字返回对应的Student对象
    Student getByKey(string key) const {
        Student s;
        if (key == "name") {
            s.name = this->name;
            s.id = "";
            s.age = 0;
        } else if (key == "id") {
            s.name = "";
            s.id = this->id;
            s.age = 0;
        } else if (key == "age") {
            s.name = "";
            s.id = "";
            s.age = this->age;
        }
        return s;
    }
};
int main()
{
    map<Student, int>m;
    Student s1 = {"Tom", "001", 18};
    m[s1] = 100;
 
    // 根据name查找
    Student s2 = s1.getByKey("name");
    auto it = m.find(s2);
    if (it != m.end()) {
        cout << "Found, value is " << it->second << endl;
    } else {
        cout << "Not found" << endl;
    }
 
    // 根据id查找
    Student s3 = s1.getByKey("id");
    it = m.find(s3);
    if (it != m.end()) {
        cout << "Found, value is " << it->second << endl;
    } else {
        cout << "Not found" << endl;
    }
 
    // 根据age查找
    Student s4 = s1.getByKey("age");
    it = m.find(s4);
    if (it != m.end()) {
        cout << "Found, value is " << it->second << endl;
    } else {
        cout << "Not found" << endl;
    }
 
    return 0;
}

假如map类m的key值是一个Student类,现在已经重载比较运算符了,如果给出name或者id或者age进行查找,为什么这段代码报错?

  • 写回答

2条回答 默认 最新

  • 月迷津渡` 2023-03-31 15:51
    关注
    
    
    #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
        {
            if(s1.name==name)
            {
                if(s1.id==id)
                {
                    return s1.age<age;
                }
                else
                {
                    return s1.id<id;
                }
            }
            else
            {
                return s1.name<name;
            }
        }
     
        // 新增函数,根据给出的关键字返回对应的Student对象
        Student getByKey(string key) const 
        {
            Student s;
            if (key == "name") {
                s.name = this->name;
                s.id = "";
                s.age = 0;
            } else if (key == "id") {
                s.name = "";
                s.id = this->id;
                s.age = 0;
            } else if (key == "age") {
                s.name = "";
                s.id = "";
                s.age = this->age;
            }
            return s;
        }
    };
    int main()
    {
        map<Student, int>m;
        Student s1 = {"Tom", "001", 18};
        m[s1] = 100;
     
        // 根据name查找
        Student s2 = s1.getByKey("name");
        auto it = m.find(s2);
        if (it != m.end()) {
            cout << "Found, value is " << it->second << endl;
        } else {
            cout << "Not found" << endl;
        }
     
        // 根据id查找
        Student s3 = s1.getByKey("id");
        it = m.find(s3);
        if (it != m.end()) {
            cout << "Found, value is " << it->second << endl;
        } else {
            cout << "Not found" << endl;
        }
     
        // 根据age查找
        Student s4 = s1.getByKey("age");
        it = m.find(s4);
        if (it != m.end()) {
            cout << "Found, value is " << it->second << endl;
        } else {
            cout << "Not found" << 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)