1.实现封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个) 再把该容器中的对象,保存到文件中。再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Stu
{
public:
string name;
string sex;
int age;
double score;
Stu(){};
Stu(string name,string sex,int age,double score):name(name),sex(sex),age(age),score(score)
{}
};
ostream &operator<<(ostream &cout,const Stu &s)
{
cout << s.name << " " << s.sex << " " << s.age << " " << s.score << endl;
return cout;
}
istream &operator>>(istream &cin,Stu &s)
{
cin >> s.name >> s.sex >> s.age >> s.score;
return cin;
}
int main()
{
vector<Stu> s;
s.push_back(Stu("LL","man",22,99));
s.push_back(Stu("ZY","man",18,96));
s.push_back(Stu("CC","woman",19,91));
s.push_back(Stu("XY","woman",17,88));
s.push_back(Stu("SX","woman",23,100));
ofstream ofs;
ofs.open("E:/HQYJ/C++/day8/zy1/stu.txt",ios::out);
for(const auto &s : s)
{
ofs << s <<endl;
}
ofs.close();
vector<Stu> loadStu;
ifstream ifs;
ifs.open("E:/HQYJ/C++/day8/zy1/stu.txt",ios::in);
Stu temp;
while(ifs >> temp)
{
loadStu.push_back(temp);
}
ifs.close();
for(const auto &s : loadStu)
{
cout << s << endl;
}
return 0;
}


2.思维导图


被折叠的 条评论
为什么被折叠?



