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

相关推荐
非凡ghost几秒前
AOMEI Partition Assistant磁盘分区工具:磁盘管理的得力助手
linux·运维·前端·数据库·学习·生活·软件需求
m0_578267862 分钟前
从零开始的python学习(九)P142+P143+P144+P145+P146
笔记·python·学习
minji...4 分钟前
C++ list的模拟实现
开发语言·c++·list
非凡ghost38 分钟前
简朴App(PlainApp):开源、隐私保护的手机管理工具
学习·智能手机·生活·软件需求
晨非辰43 分钟前
#C语言——刷题攻略:牛客编程入门训练(十):攻克 循环控制(二),轻松拿捏!
c语言·开发语言·经验分享·学习·visual studio
零点零一1 小时前
`vcpkg` 微软开源的 C/C++ 包管理工具的使用和安装使用spdlog
c语言·c++·microsoft
wangwangblog2 小时前
LLVM 数据结构简介
开发语言·数据结构·c++
John_ToDebug2 小时前
浏览器稳定性提升之路:线上崩溃率优化中的 Return 与 CHECK 之争
c++·chrome
有谁看见我的剑了?2 小时前
k8s-临时容器学习
学习·容器·kubernetes
dragoooon342 小时前
[优选算法专题二——NO.16最小覆盖子串]
c++·算法·leetcode·学习方法