9.8C++作业

思维导图

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

class Stu
{
    friend ofstream &operator<<(ofstream &ofs,const Stu &stu);
private:
    string name;
    string id;
    int age;
    double score;
public:
    Stu(){}
    Stu(string name, string id, int age, double score):name(name),id(id),age(age),score(score)
    {}
    ~Stu(){}
    void show() const
    {
        cout << name << " " << id << " " << age << " " << score <<endl;
    }
};

ofstream &operator<<(ofstream &ofs,const Stu &stu)
{
    ofs << "name:" << stu.name << " id:" << stu.id << " age:" << stu.age << " score:" << stu.score << endl;
    return ofs;
}

int main()
{
    vector<Stu> s1;
    s1.push_back(Stu("yy","1001",19,97));

    vector<Stu> s2;
    s2.push_back(Stu("dd","1002",19,94));

    vector<Stu> s3;
    s3.push_back(Stu("hh","1003",20,96));

    ofstream ofs;
    ofs.open("D:\\Qt\\qt-project\\hq\\day8\\test6\\stu_txt",ios::out);

    for(const auto &stu : s1)
    {
        ofs << stu << endl;
    }
    for(const auto &stu : s2)
    {
        ofs << stu << endl;
    }
    for(const auto &stu : s3)
    {
        ofs << stu << endl;
    }

    ofs.close();

    ifstream ifs;
    ifs.open("D:\\Qt\\qt-project\\hq\\day8\\test6\\stu_txt",ios::in);
    char buf[1024];
    while(ifs >> buf)
    {
        cout << buf << endl;
    }

    ifs.close();
    return 0;
}
cpp 复制代码
#include <iostream>
#include <list>

using namespace std;

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

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

    cout << "l1:";
    list_printf(l1);
    l1.pop_back();
    list_printf(l1);
    cout << "==============" << endl;

    list<int> l2 = l1;
    cout << "l2:";
    list_printf(l2);
    cout << "==============" << endl;

    list<int> l3(l1.begin(),l1.end());
    cout << "l3:";
    list_printf(l3);
    cout << "==============" << endl;

    list<int> l4(6,6);
    cout << "l4:";
    list_printf(l4);
    l4.swap(l2);
    cout << "==============" << endl;
    cout << "l4:";
    list_printf(l4);
    cout << "l2:";
    list_printf(l2);
    cout << "==============" << endl;

    list<int> l5;
    l5.assign(l1.begin(),l1.end());
    cout << "l5:";
    list_printf(l5);
    cout << "==============" << endl;
    if(!l5.empty())
    {
        cout << l5.size() << endl;
    }
    l5.resize(7,9);
    list_printf(l5);
    l5.resize(3);
    list_printf(l5);
    cout << "==============" << endl;

    list<int> l6 = l4;
    cout << "l6:";
    list_printf(l6);
    l6.remove(20);
    list_printf(l6);
    l6.clear();
    list_printf(l6);
    return 0;
}
相关推荐
杨小码不BUG22 分钟前
小鱼的数字游戏:C++实现与算法分析(洛谷P1427)
c++·算法·数组·信奥赛·csp-j/s
高山有多高23 分钟前
栈:“后进先出” 的艺术,撑起程序世界的底层骨架
c语言·开发语言·数据结构·c++·算法
蔗理苦26 分钟前
2025-10-07 Python不基础 19——私有对象
开发语言·python·私有对象
普罗米修斯42 分钟前
C++ 设计模式理论与实战大全【共73课时】
c++·后端
普罗米修斯1 小时前
C++ 设计模式原理与实战大全-架构师必学课程 | 完结
c++·后端
greentea_20131 小时前
Codeforces Round 65 C. Round Table Knights(71)
c语言·开发语言·算法
小秋学嵌入式-不读研版1 小时前
C61-结构体数组
c语言·开发语言·数据结构·笔记·算法
Evand J2 小时前
组合导航的MATLAB例程,二维平面上的CKF滤波,融合IMU和GNSS数据,仿真,观测为X和Y轴的坐标,附代码下载链接
开发语言·matlab·平面·imu·组合导航
蔗理苦2 小时前
2025-10-07 Python不基础 20——全局变量与自由变量
开发语言·python
夜月yeyue2 小时前
个人写HTOS移植shell
c++·mcu·算法·性能优化·架构·mfc