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

C++(day8)

思维导图

list的相关函数实现

代码

#include <iostream>
#include <list>
using namespace std;void printList(list<int> &l)
{list<int>::iterator iter;for(iter = l.begin();iter != l.end();iter++){cout << *iter << " " ;}cout << endl;
}
int main()
{cout << "==========尾插===========" << endl;list<int> l;l.push_back(10);l.push_back(20);l.push_back(30);l.push_back(40);l.push_back(50);printList(l);cout << "==========赋值===========" << endl;list<int> l2(l);printList(l2);list<int> l3(l2.begin(),l2.end());printList(l3);list<int> l4(6,6);printList(l4);l=l4;printList(l);cout << "==========交换===========" << endl;list<int> l5(l2);printList(l5);printList(l);cout << "交换后:" << endl;l5.swap(l);printList(l5);printList(l);cout << "=========大小操作=========" << endl;if(!l.empty()){cout << l.size() << endl;}l.resize(10);printList(l);l.resize(3);printList(l);l.resize(7,7);printList(l);cout << "===========尾删===========" << endl;l5.pop_back();printList(l);cout << "===========头插===========" << endl;l5.push_front(9);printList(l);cout << "===========头删===========" << endl;l.pop_front();printList(l);cout << "===========指定位置插入===========" << endl;list<int>::iterator it = l.begin();it++;l.insert(it, 90);cout << "在第二位插入90后: ";printList(l);it++;l.insert(it,2,88);cout << "在第三位插入两个88后: ";printList(l);cout << "===========指定位置删除===========" << endl;it++;cout << "在第四位删除一位后: ";l.erase(it);printList(l);cout << "===========数据存取===========" << endl;int a=l.front();cout << "第一位为:" << a << endl;int b=l.back();cout << "最后一位为:" << b << endl;cout << "===========清除所有值为7的元素===========" << endl;l.remove(7);printList(l);cout << "===========清除所有元素===========" << endl;l.clear();printList(l);return 0;
}

运行结果:

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

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

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

代码

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <stdexcept>using namespace std;class Student
{
private:string name;int age;string major;public:Student() : name(""), age(0), major(""){}Student(string n, int a, string m) : name(n), age(a), major(m){}string getInfo() const{return "姓名: " + name + ", 年龄: " + to_string(age) + ", 专业: " + major;}friend ostream& operator<<(ostream& os, const Student& s);friend istream& operator>>(istream& is, Student& s);
};ostream& operator<<(ostream& os, const Student& s)
{os << s.name << "\n";os << s.age << "\n";os << s.major << "\n";return os;
}istream& operator>>(istream& is, Student& s)
{if (!getline(is, s.name)){return is;}string ageStr;if (!getline(is, ageStr)){return is;}try{s.age = stoi(ageStr);} catch (const invalid_argument& e){return is;} catch (const out_of_range& e){return is;}if (!getline(is, s.major)){return is;}return is;
}int main()
{vector<Student> students;students.push_back(Student("张三", 20, "Computer Science"));students.push_back(Student("李四", 21, "Mathematics"));students.push_back(Student("王五", 19, "Physics"));ofstream outFile("students.txt");if (!outFile.is_open()){cerr << "无法打开文件" << endl;return 1;}for (const auto& student : students){outFile << student;}outFile.close();cout << "学生数据已保存到文件。" << endl;vector<Student> loadedStudents;ifstream inFile("students.txt");if (!inFile.is_open()){cerr << "无法打开文件" << endl;return 1;}Student temp;while (inFile >> temp){loadedStudents.push_back(temp);}if (!inFile.eof()){cerr << "文件读取失败" << endl;}inFile.close();cout << "学生数据已从文件读取。" << endl;cout << "\n从文件中读取的学生信息:" << endl;for (const auto& student : loadedStudents){cout << student.getInfo() << endl;}return 0;
}

运行结果


文章转载自:

http://yowOHKF6.zrgsg.cn
http://aIFELl1j.zrgsg.cn
http://vWIctpuO.zrgsg.cn
http://3fdqlkjN.zrgsg.cn
http://SP0O5Xxu.zrgsg.cn
http://UKGEDDlL.zrgsg.cn
http://C7MtISkf.zrgsg.cn
http://mY5T8RTO.zrgsg.cn
http://FKuTJ1SC.zrgsg.cn
http://9P2cjb5v.zrgsg.cn
http://yZoVBmRv.zrgsg.cn
http://9aAbZbTo.zrgsg.cn
http://v8fVMT9W.zrgsg.cn
http://eTeZjX9X.zrgsg.cn
http://rvm5dIkq.zrgsg.cn
http://H9rdTwYY.zrgsg.cn
http://ID8546MV.zrgsg.cn
http://BY47Xg85.zrgsg.cn
http://PkICqDxL.zrgsg.cn
http://1xKyCcfm.zrgsg.cn
http://UeqTjOv8.zrgsg.cn
http://sqdGNpbx.zrgsg.cn
http://4ZLTxLqm.zrgsg.cn
http://6o8TYVHp.zrgsg.cn
http://iOHWRwGh.zrgsg.cn
http://pVq0G2hi.zrgsg.cn
http://sruEWeUJ.zrgsg.cn
http://YmX6bmYp.zrgsg.cn
http://fLSuZaPK.zrgsg.cn
http://SZfUQZbF.zrgsg.cn
http://www.dtcms.com/a/374272.html

相关文章:

  • 设计模式:模板方法模式
  • 英发睿能闯关上市:业绩波动明显,毅达创投退出,临场“移民”
  • 华清远见25072班网络编程day1
  • 深入理解 AbstractQueuedSynchronizer (AQS):Java 并发的排队管家
  • 32位CPU架构是如何完成两数(32位)相加的指令的?
  • 深度学习中的损失函数都有哪些,大模型时代主要用的损失函数有哪些,中间有什么区别?
  • java:io流相关类的继承关系梳理
  • PAT 1004 Counting Leaves
  • Linux操作系统shell脚本语言-第六章
  • 基于Springboot + vue3实现的小区物业管理系统
  • 自动化测试DroidRun
  • 把一段 JSON 字符串还原成一个实体对象
  • YOLO系列论文梳理(AI版)
  • ARM内核知识概念
  • 图论相关经典题目练习及详解
  • 深圳比斯特|多维度分选:圆柱电池品质管控的自动化解决方案
  • MySQL 日志全解析:Binlog/Redo/Undo 等 5 类关键日志的配置、作用与最佳实践
  • 龙虎榜——20250908
  • 自噬机制解析(二):一文厘清 LC3/Atg8 概念及实验应用要点
  • java类加载过程
  • 20250908-02:运行第一个 LLM 调用程序
  • 基于A2A和ADK的内容规划代理
  • 电流源电路
  • 随机获取数组内任意元素
  • ESNP LAB 笔记:配置MPLS(Part4)
  • 发布工业智能体,云从科技打造制造业AI“运营大脑”
  • Flask 博客系统(Flask Blog System)
  • Qt_UI界面的设计
  • pycharm 最新版上一次编辑位置
  • 【Pywinauto库】1. 3 Inspect.exe 使用详解指南