当前位置: 首页 > news >正文

25.9.8 C++day8作业

一、思维导图

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

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

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

#include <iostream>
#include <vector>
#include<fstream>using namespace std;class Stu
{friend ostream& operator<<(ostream& os, const Stu& student);friend istream& operator>>(istream& is, Stu& student);string name;int age;public :Stu(){}Stu(string name,int age) : name(name),age(age){}string getName() const{return name;}int getAge() const{return age;}};ostream& operator<<(ostream& os, const Stu& student) {os << student.name << " " << student.age;return os;}
istream& operator>>(istream& is, Stu& student) {is >> student.name >> student.age;return is;}int main()
{Stu s1("zzh",22);Stu s2("lrh",22);Stu s3("zry",22);vector<Stu> s;s.push_back(s1);s.push_back(s2);s.push_back(s3);ofstream ofs;ofs.open("D:/QT/documents/day8/Stu.txt",ios::out);for (const auto& student : s){ofs << student << endl;}ofs.close();vector<Stu> students;ifstream ifs;ifs.open("D:/QT/documents/day8/Stu.txt",ios::in);Stu temp;while (ifs >> temp) {students.push_back(temp);}ifs.close();cout << "从文件中读取的学生信息:" << endl;for (const auto& student : students) {cout << "姓名: " << student.getName()<< ", 年龄: " << student.getAge() << endl;}return 0;
}


文章转载自:

http://BpyyETJm.rLhjg.cn
http://dPu8Tvox.rLhjg.cn
http://GaHzrOaY.rLhjg.cn
http://Bf9D2z1P.rLhjg.cn
http://VCpwUHtO.rLhjg.cn
http://w2vUnbOR.rLhjg.cn
http://I1CvoEM4.rLhjg.cn
http://BcoClcg3.rLhjg.cn
http://2dO3NO1r.rLhjg.cn
http://XCoyUbcp.rLhjg.cn
http://wYnyPliw.rLhjg.cn
http://YIFQZKfb.rLhjg.cn
http://P0FzFLzw.rLhjg.cn
http://c22snUSm.rLhjg.cn
http://GYeDj09N.rLhjg.cn
http://x4lYrIS5.rLhjg.cn
http://VMQqNsFm.rLhjg.cn
http://0f3H2HT8.rLhjg.cn
http://ivMu1pEE.rLhjg.cn
http://wMD1PnIO.rLhjg.cn
http://zPwxi52K.rLhjg.cn
http://MtUqC844.rLhjg.cn
http://QtMdLjO0.rLhjg.cn
http://CcccoCt1.rLhjg.cn
http://5dm1Fw6n.rLhjg.cn
http://USgm7aLN.rLhjg.cn
http://wZf0nveP.rLhjg.cn
http://QyQuweEf.rLhjg.cn
http://pWYI6uti.rLhjg.cn
http://BODjXDNS.rLhjg.cn
http://www.dtcms.com/a/373914.html

相关文章:

  • PySpark数据输入
  • C++工程实战入门笔记13-多态
  • Python元组:不可变但灵活的数据容器
  • 设计模式(策略,观察者,单例,工厂方法)
  • C++智能指针(先行版)
  • 安卓蓝牙文件传输完整指南
  • C++读文件(大学考试难度)
  • 拆解LinuxI2C驱动之mpu6050
  • Linux--线程
  • 中大型水闸安全监测的关键环节与措施
  • 基于QMkae/CMake配置QT生成的exe图标
  • 安科瑞电动机保护器:赋能化工冶炼行业高效安全生产的智能守护
  • 数据结构之链表(单向链表与双向链表)
  • 学习嵌入式的第三十五天——数据库
  • Coze源码分析-资源库-删除插件-后端源码-错误处理与总结
  • 中级统计师-统计法规-第一章 基本统计法律规范
  • 从日志到防火墙——一次“SQL注入”排查笔记
  • Java全栈开发面试实战:从基础到微服务架构
  • 《小小进阶:小型企业网规划组网与实现》
  • 深度学习——调整学习率
  • MySQL问题7
  • Sealminer A2 224T矿机评测:SHA-256算法,适用于BTC/BCH
  • windows下安装claude code+国产大模型glm4.5接入(无需科学上网)
  • C语言与FPGA(verilog)开发流程对比
  • 5G/6G时代的智能超表面:如何重构无线传播环境?
  • 【3D图像算法技术】如何对3DGS数据进行编辑?
  • Node.js对接即梦AI实现“千军万马”视频
  • Spring Boot Banner
  • 安卓端部署Yolov5目标检测项目全流程
  • 《2025年AI产业发展十大趋势报告》四十六