list<struct> c.erase
#include<iostream>#include<list>
#include<string>
using namespace std;
struct students
{
string name;
int num;
string sex;
int age;
};
////////////////////////////////////
void input(list<students> &s);
void del(list<students> &s);
void show(list<students> s);
////////////////////////////////////
void input(list<students> &s)
{
int k=1;
students temp;
while (0!=k)
{
cout<<"姓名:";
cin>>temp.name;
cout<<"学号:";
cin>>temp.num;
cout<<"性别:";
cin>>temp.sex;
cout<<"年龄:";
cin>>temp.age;
s.push_back(temp);
cout<<"继续(1) 结束(0)"<<endl;
cin>>k;
}
}
//------------------------------------------------------
void del(list<students> &s, const int age)
{
list<students>::iterator i=s.begin();
while (i!= s.end())
{
if(age==i->age)
{
s.erase(i);
}
++i;
}
}
//------------------------------------------------------------
void show(list<students> s)
{
cout<<"姓名\t"<<"学号\t"<<"性别\t"<<"年龄\t"<<endl;
list<students>::iterator it=s.begin();
while (s.end()!=it)
{
cout<<it->name<<"\t"<<it->num<<"\t"<<it->sex<<"\t"<<it->age<<endl;
++it;
}
}
/////////////////////////////////////////////////////////////
void main()
{
list<students> data;
input(data);
system("cls");
show(data);
int a;
cout<<"输入要删除的年龄"<<endl;
cin>>a;
del(data,a);//编译能通过,但到了这里程序崩溃了~~求高手指点~~~
show(data);
}