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


#include 
#include 
#include 
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()
{
    mapint>m;
    return 0;
}

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

基于new bing 编写的答案
根据您的代码,您已经重载了Student类的小于运算符,使得Student类的对象可以按照name、id和age的优先级进行比较。这样,您就可以使用map类的find方法来根据给出的关键字来在m中进行哈希查找。

例如,如果您想要查找name为"Tom",id为"123",age为20的学生在m中是否存在,您可以这样写:

Student s; s.name = “Tom”; s.id = “123”; s.age = 20; auto it = m.find(s); // 返回一个迭代器指向m中与s相等的键值对,如果没有则指向m.end() if (it != m.end()) // 如果找到了 { cout << "Found: " << it->first.name << " " << it->first.id << " " << it->first.age << " -> " << it->second << endl; // 输出键值对 } else // 如果没有找到 { cout << “Not found” << endl; }

注意,如果您只给出了name或者id或者age中的一个或两个,那么您需要给另外的属性赋一个默认值,比如空字符串或者0,以便能够正确地进行比较。

希望这能帮到您。👍

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:

在C++中,std::map是基于关键字排序的关联容器,因此在使用自定义对象作为key时,需要定义该对象的小于运算符(operator<)或者函数对象(Functor),这样std::map才能确定对象的大小关系,进行元素的排序和查找。

在上述你的代码中,已经定义了一个函数对象Student,可以直接作为mapkey,因此只需要在Student类中增加一个函数,根据给出的关键字返回对应的Student对象,即可实现根据不同的关键字进行查找。

具体您可以看你一下代码:

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;
}

如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/738481
  • 除此之外, 这篇博客: Go 学习笔记(12)— 字典map定义、初始化、读取字典、删除字典、清空字典、map 按 key 进行有序遍历中的 4. 遍历字典 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 注意:遍历输出元素的顺序与填充顺序无关,不能期望 map 在遍历时返回某种期望顺序的结果。

    package main
    
    import "fmt"
    
    func main() {
    	book := make(map[string]string)
    
    	book["price"] = "100"
    	book["author"] = "wohu"
    	book["language"] = "Chinese"
    
    	for k := range book { // 迭代 key, 不能保证每次迭代元素的顺序
    		fmt.Println(k, "value is ", book[k])
    	}
    
    	for k, v := range book { // 同时迭代 key 和 value
    		fmt.Println(k, v)
    	}
    
    	/*查看元素在集合中是否存在 */
    	published, ok := book["published"] /*如果确定是真实的,则存在,否则不存在 */
    	/*fmt.Println(published) */
    	/*fmt.Println(ok) */
    	if ok {
    		fmt.Println("published is ", published)
    	} else {
    		fmt.Println("published not exist")
    	}
    }
    

    运行结果为:

    price value is  100
    author value is  wohu
    language value is  Chinese
    price 100
    author wohu
    language Chinese
    published not exist
    

    读取字典,就是根据 key 值,在字典中查找 key 对应的 value 值,读取字典通常有两种方法,分别是:

    // 第一种情况,断言查询,推荐使用这种方法
    val,ok := map[key]
    
    // 第二种情况直接查询
    val := map[key]
    

    上边两种读取字典的方法中,

    • 第一种采用断言的方式,当 oktrue 时,表示 key 存在于字典中,当 okfalse 时,表示 key 不存在于字典中;
    • 第二种情况直接查询 key 值的方式,如果 key 不在 map 种时,返回值是字典定义中 value 的类型默认初始值。如 value 的类型为 int,则默认值为 0,如果 value 类型是指针,则默认是 nil