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;
}

思维导图

相关推荐
睡美人的小仙女1273 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
程序员徐师兄3 小时前
Windows JDK11 下载安装教程,适合新手
java·windows·jdk11 下载安装·jdk11 下载教程
rayufo3 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk3 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
缺点内向4 小时前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
一起养小猫4 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
zhougl9964 小时前
Java 所有关键字及规范分类
java·开发语言
java1234_小锋5 小时前
Java高频面试题:MyISAM索引与InnoDB索引的区别?
java·开发语言
2501_944525545 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
Bella的成长园地5 小时前
面试中关于 c++ async 的高频面试问题有哪些?
c++·面试