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 (将#修改为@)