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

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值