c++学习:list链表模板类实战(学生管理系统)
•
编程语言
要求:
- 编写一个学生结构体
- 要求面向对象
- 要求用到链表连接每个学生
代码
#include
#include
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"(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<>mode;
switch (mode) {
case Mode_Add:
{
Student s;
cout<>s; //operator>>(cin,s);
list.push_back(s);
}
break;
case Mode_Show:
{
for(std::list::iterator it=list.begin(); it!=list.end(); it++)
{
it->show();
}
}
break;
case Mode_Sort:
list.sort(cmp);
break;
case Mode_Delete:
{
cout<>delName;
std::list::iterator it;
for(it=list.begin(); it!=list.end(); it++)
{
if(it->getName() == delName)
{
//删除
cout<getName():"<getName()<<endl;
list.erase(it);
break;
}
}
}
break;
}
}
}
private:
list 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的话是降序
-
list.erase(it);擦除迭代器后就不能++了,所以要break,所以只能删除一个
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/35c53f9f77.html
