c++学习:list链表模板类实战(学生管理系统)

要求:

  • 编写一个学生结构体
  • 要求面向对象
  • 要求用到链表连接每个学生

代码

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

class Student
{
    friend istream& operator>>(istream&in,Student &ra);
    friend bool cmp(Student &a, Student &b);
public:
    Student(){}
    Student(string n,int a,float s):name(n),age(a),score(s){}

    void show()
    {
        cout<<name<<"\t"<<age<<"\t"<<score<<endl;
    }
    string getName()
    {
        return name;
    }
private:
    string name;
    int age;
    float score;
};

istream& operator>>(istream&in,Student &ra)
{
    in>>ra.name>>ra.age>>ra.score;
    return in;
}

bool cmp(Student &a, Student &b)
{
    if(a.age<b.age)
        return true;
    else
        return false;
}

//应用程序管理类
class Application
{
    enum App_MODE
    {
        Mode_Add = 1,
        Mode_Show,
        Mode_Sort,
        Mode_Delete,
    };
public:
    int exec()
    {
        while(1)
        {
            int mode;
            cout<<"[1]add [2]show [3]sort [4]delete:";
            cin>>mode;

            switch (mode) {
                case Mode_Add:
                        {
                            Student s;
                            cout<<"name age score:";
                            cin>>s; //operator>>(cin,s);
                            list.push_back(s);
                        }
                        break;
                case Mode_Show:
                        {
                            for(std::list<Student>::iterator it=list.begin(); it!=list.end(); it++)
                            {
                                it->show();
                            }
                        }
                    break;
                case Mode_Sort:
                    list.sort(cmp);

                    break;
                case Mode_Delete:
                    {
                        cout<<"please input del name:";
                        string delName;
                        cin>>delName;
                        std::list<Student>::iterator it;
                        for(it=list.begin(); it!=list.end(); it++)
                        {
                            if(it->getName() == delName)
                            {
                                //删除
                                cout<<"it->getName():"<<it->getName()<<endl;
                                list.erase(it);
                                break;
                            }
                        }
                    }

                    break;
            }
        }
    }

private:
    list<Student> list;
};

int main()
{
    Application app;

    return app.exec();
}

注意

  • list.sort(cmp);是调用bool cmp(Student &a, Student &b)这个函数,cmp是一个函数对象,等于回调这个函数,函数必须要bool cmp(const Type1 &a, const Type2 &b)这种格式

  • 在排序函数中if(a.age<b.age),是表示升序,>的话是降序

  • list.erase(it);擦除迭代器后就不能++了,所以要break,所以只能删除一个

相关推荐
jjkkzzzz2 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
985小水博一枚呀2 小时前
【AI大模型学习路线】第二阶段之RAG基础与架构——第七章(【项目实战】基于RAG的PDF文档助手)技术方案与架构设计?
人工智能·学习·语言模型·架构·大模型
pystraf2 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
Funny-Boy2 小时前
菱形继承原理
c++
hello1114-4 小时前
Redis学习打卡-Day3-分布式ID生成策略、分布式锁
redis·分布式·学习
小Tomkk4 小时前
2025年PMP 学习二十 第13章 项目相关方管理
学习·pmp·项目pmp
Nobkins4 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论
独行soc4 小时前
2025年渗透测试面试题总结-百度面经(题目+回答)
运维·开发语言·经验分享·学习·面试·渗透测试·php
海棠蚀omo4 小时前
C++笔记-红黑树
开发语言·c++·笔记
ysy16480672395 小时前
03算法学习_977、有序数组的平方
学习·算法