9.8作业

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.思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值