【发布时间】:2012-12-10 14:27:13
【问题描述】:
我正在尝试使用地图将数据安全保存到文件中,但我不知道如何。 我想将学生的姓名和年龄保存到文件中,然后当我查找学生的姓名时,它应该显示他们的年龄。
#include <iostream>
#include <map>
#include <fstream>
#include <string>
using namespace std;
class student {
private:
map<int, string> map;
public:
void students(string name, int age);
};
void students(string name, int age) {
if (age < 1) {
cout << "You must enter a positive number." << endl;
return;
}
}
void main() {
ofstream filemap;
filemap.open("map.txt");
int age;
string name;
cout << "Please enter the name : " << endl;
cin >> name;
cout << "Please enter the age : " << endl;
cin >> age;
// code to save map to file
filemap.close();
}
【问题讨论】:
-
如果这是一个使用地图的地方(我不这么认为),肯定应该是
map<string,int>,而不是map<int,string>。一般来说,名字比年龄更独特。 -
谢谢。我的意思是这样输入。