微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C ++在地图中查找元素

如何解决C ++在地图中查找元素

你好

typedef map<MedDept,int,findDepartment> temp;
typedef temp::value_type temp_type;
     
MedDept md1("Test1",3,"Physio info");
MedDept md2("Test2",4,"info");
     
temp t;
    
t.insert(temp_type(md1,2));
t.insert(temp_type(md2,3));

cout << t.size() << endl; // 2

但是,当我尝试如下查找其中一个元素时,上面的代码运行完美:

if (t.find(md2) != t.end()) {
   cout << "found" << endl; 
} else {
   cout << "not found" << endl;
}

找不到返回

但是当尝试如下查找md1时。

if (t.find(md1) != t.end()) {
   cout << "found" << endl; 
} else {
   cout << "not found" << endl;
}

它可以工作并返回对象。

关于可能存在问题的任何想法。谢谢

这是比较运算符

bool findDepartment::operator( ) ( const MedDept & md1,const MedDept & md2 ) const {
    return (md1.getDepartmentName().compare(md2.getDepartmentName()) != 0);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。