自己用map容器存储了一些东西,不知道如何写入文件,而且需要读取文件中的内容!!!
2 回答
onemoo
TA贡献883条经验 获得超454个赞
using std::map;
using std::ofstream;
using std::ifstream;
map<int, int> m = { ... }; // 待存储的map m
// 存入文件out.txt
ofstream of("out.txt");
for (const auto &i : m) {
of << i.first << ' ' << i.second << std::endl;
}
// 读取文件,存入map m2中
map<int, int> m2;
ifstream if("out.txt");
int key, value;
while (if >> key >> value) {
m2[key] = value;
}- 2 回答
- 0 关注
- 5773 浏览
添加回答
举报
0/150
提交
取消
