c++ map容器储存自定义对象报错
#include
#include
#include
using namespace std;
class Person
{
public:
string m_Name;
int m_Age;
Person(int age = 0, string name = "NULL")
{
m_Age = age;
m_Name = name;
}
};
class Address
{
public:
string m_Community;
int m_Floor;
Address(int floor = 0, string community = "NULL")
{
m_Community = community;
m_Floor = floor;
}
};
class ComparePerson
{
public:
bool operator()(const Person &p1, const Person &p2) const
{
return p1.m_Age > p2.m_Age;
}
};
void printPersonMap(const map &m)
{
for (map::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "Name: " << it->first.m_Name << " Age:" << it->first.m_Age << " Community: " << it->second.m_Community << " Floor" << it->second.m_Floor << endl;
}
cout << endl;
}
void test07()
{
Person p1(1, "aaa");
Person p2(2, "bbb");
Person p3(3, "ccc");
Person p4(4, "ddd");
Address a1(10, "abc");
Address a2(20, "bcd");
Address a3(30, "cde");
Address a4(40, "def");
map m;
m.insert(pair(p1, a1));
m.insert(pair(p2, a2));
m.insert(pair(p3, a3));
m.insert(pair(p4, a4));
printPersonMap(m);
}
int main()
{
test07();
return 0;
}
第四十二行有个MyCompare改一下
自定义比较器有问题