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

C++STL map容器这样的写法为啥报错
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- _花花 2021-02-03 18:16关注
1,#include<string>
2,class Person定义放到void printMap(map<int,Person>&m) 前
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用