#include<map>
#include <iostream>
using namespace std;
void printMap(map<int,Person>&m) {
for (map<int, Person>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << it->first << " Person value 姓名:" << it->second.m_Name << "Person value 年龄:" << it->second.m_Age << endl;
}
}
class Person {
public:
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
map<int, Person>m;
Person p1("刘备", 20);
Person p2("张飞", 21);
Person p3("关羽", 25);
Person p4("赵云", 26);
m.insert(make_pair(1, p1));
}
int main()
{
test01();
system("pause");
return 0;
}
1,#include<string>
2,class Person定义放到void printMap(map<int,Person>&m) 前
没有什么问题啊。