自己用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 關注
- 5745 瀏覽
添加回答
舉報
0/150
提交
取消
