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,所以只能删除一个

相关推荐
Once_day15 分钟前
C++之《程序员自我修养》读书总结(1)
c语言·开发语言·c++·程序员自我修养
Trouvaille ~25 分钟前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
觉醒大王26 分钟前
强女思维:着急,是贪欲外显的相。
java·论文阅读·笔记·深度学习·学习·自然语言处理·学习方法
坚果派·白晓明36 分钟前
在鸿蒙设备上快速验证由lycium工具快速交叉编译的C/C++三方库
c语言·c++·harmonyos·鸿蒙·编程语言·openharmony·三方库
小镇敲码人43 分钟前
深入剖析华为CANN框架下的Ops-CV仓库:从入门到实战指南
c++·python·华为·cann
YCY^v^1 小时前
JeecgBoot 项目运行指南
java·学习
云小逸1 小时前
【nmap源码解析】Nmap OS识别核心模块深度解析:osscan2.cc源码剖析(1)
开发语言·网络·学习·nmap
张张努力变强2 小时前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
小镇敲码人2 小时前
探索CANN框架中TBE仓库:张量加速引擎的优化之道
c++·华为·acl·cann·ops-nn
平安的平安2 小时前
面向大模型算子开发的高效编程范式PyPTO深度解析
c++·mfc