C++day8作业

封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个)

再把该容器中的对象,保存到文件中。

再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。

cpp 复制代码
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;


class Stu
{
private:
    string name;
    int age;
    int id;
public:
    Stu(){}
    Stu(string name,int age,int id):name(name),age(age),id(id){}
    string wri()const
    {
        return name + " " + to_string(age) + " " + to_string(id);
    }
};
int main()
{
    vector<Stu> stus;
    stus.push_back(Stu("张三",20,1001));
    stus.push_back(Stu("李四",21,1002));
    stus.push_back(Stu("王五",20,1003));
    ofstream ofs;
    try {
        ofs.open("D:/QT/QT/day8/aaa",ios::out);
        if(!ofs.is_open())
        {
            throw -1;
        }
    } catch (int &e) {
        if(e==-1)
        {
            cout << "打开失败" << endl;
        }
    }

    for(auto &stu : stus)
    {
        ofs << stu.wri() <<endl;
    }
    ofs.close();

    ifstream ifs;
    try {
        ifs.open("D:/QT/QT/day8/aaa",ios::in);
        if(!ifs.is_open())
        {
            throw -1;
        }
    } catch (int &c) {
        if(c==-1)
        {
            cout << "打开失败" << endl;
        }
    }

    char buf[1024];
    while(ifs>>buf)
    {
        cout << buf << endl;
    }

    ifs.close();
    return 0;
}

实现list的相关函数

cpp 复制代码
#include <iostream>
#include <list>
using namespace std;

void printfList(list<int> &lst)
{
    list<int>::iterator iter;
    for(iter = lst.begin(); iter != lst.end();iter++)
    {
        cout << *iter << " ";
    }
    cout << endl;
}

int main()
{
    list<int> lst;
    lst.push_back(10);
    lst.push_back(20);
    lst.push_back(30);
    lst.push_back(40);
    lst.push_back(50);

    printfList(lst);

    list<int>  lst1;
    lst1=lst;
    printfList(lst1);
    list<int>  lst2(lst1);
    printfList(lst2);
    list<int> lst3;
    lst3.assign(lst2.begin(),lst2.end());
    printfList(lst3);

    if(!lst.empty())
    {
        cout << "aaa" <<endl;
        cout << lst.size() << endl;
    }
    cout << "===============" <<endl;
    lst.resize(6,5);
    printfList(lst);
    cout << "===============" <<endl;
    printfList(lst1);
    lst1.pop_back();
    printfList(lst1);
    cout << "===============" <<endl;
    printfList(lst2);
    lst2.insert(lst2.begin(),90);
    printfList(lst2);
    cout << "===============" <<endl;
    printfList(lst3);
    lst3.clear();
    printfList(lst3);
    return 0;
}

思维导图

相关推荐
LawrenceLan36 分钟前
Flutter 零基础入门(十一):空安全(Null Safety)基础
开发语言·flutter·dart
txinyu的博客1 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
码不停蹄Zzz1 小时前
C语言第1章
c语言·开发语言
猫头虎1 小时前
如何在浏览器里体验 Windows在线模拟器:2026最新在线windows模拟器资源合集与技术揭秘
运维·网络·windows·系统架构·开源·运维开发·开源软件
行者962 小时前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
阿蒙Amon2 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
SmartRadio2 小时前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
且去填词2 小时前
Go 语言的“反叛”——为什么少即是多?
开发语言·后端·面试·go
知乎的哥廷根数学学派3 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
yeziyfx3 小时前
kotlin中 ?:的用法
android·开发语言·kotlin