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

相关推荐
StarPrayers.5 分钟前
损失函数(Loss Function)、反向传播(Backward Propagation)和优化器(Optimizer)学习笔记
人工智能·笔记·深度学习·学习
孤廖12 分钟前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
小龙报42 分钟前
《算法通关指南---C++编程篇(3)》
开发语言·c++·算法·visualstudio·学习方法·visual studio
Mr_WangAndy1 小时前
C++设计模式_行为型模式_状态模式State
c++·设计模式·状态模式
郝学胜-神的一滴1 小时前
Effective STL 第5条:区间成员函数优先于单元素成员函数
开发语言·c++·程序人生·stl·软件工程
mit6.8242 小时前
c++|表达最值的更好方法|clamp
c++
涤生z2 小时前
list.
开发语言·数据结构·c++·学习·算法·list
FFZero13 小时前
【C++/Lua联合开发】 (三) C++调用Lua
c++·junit·lua
Source.Liu3 小时前
【BuildFlow & 筑流】品牌命名与项目定位说明
c++·qt·rust·markdown·librecad
bkspiderx3 小时前
C++设计模式之行为型模式:访问者模式(Visitor)
c++·设计模式·访问者模式