操作时数据删除
当我初始化完数据(比如说1 1和2 2)后进行查找,输入0正常打印,输入1打印第二个数据,而且再一次查找就会报错,打印数据也消失了
下面是部分代码
struct stu {
string name;
int score;
};
void opList() {
list<stu>data_l;
struct stu t;
struct stu p;
int a, b;
//initialization
cout << "输入初始数据个数" << endl;
cin >> a;
for (int i = 0; i < a; i++) {
cout << "输入第" << i + 1 << "个数据" << endl;
cin >> p.name >> p.score;
data_l.push_back(p);
}
system("cls");
menu();
list<stu>::iterator it = data_l.begin();
//op
while (1) {
cin >> a;
switch (a) {
case 1://add
cout << "输入序号在其后添加" << endl;
cin >> b;
if (b > size(data_l)) {
cout << "错误的数字" << endl;
system("pause");
system("cls");
menu();
break;
}
cout << "输入添加的姓名和分数" << endl;
cin >> t.name >> t.score;
data_l.push_back(t);
system("cls");
menu();
continue;
case 2://del
continue;
case 3://change
continue;
case 4://find
cout << "输入查找的数据的序号,输入0打印当前数据" << endl;
cin >> b;
if (b == 0) {
cout << "姓名\t分数" << endl;
for (; it != data_l.end(); ++it) {
cout << it->name << "\t" << it->score << endl;
}
system("pause");
system("cls");
menu();
continue;
}
for (int i = 0; i < b; i++, it++);
cout << "查找的数据为:" << it->name << "\t" << it->score << endl;
system("pause");
system("cls");
menu();
continue;
case 5://return
return;
case 6://quit
exit(0);
}
}
}
说一下题
删除数据有个迭代器失效的现象,
自行解决
可能有bug但是跑起来了
(附上全部代码)
void opList() {
list<struct stu>data_l;
list<struct stu>::iterator it;
struct stu t;
struct stu p;
int a, b;
//initialization
cout << "输入初始数据个数" << endl;
cin >> a;
for (int i = 0; i < a; i++) {
cout << "输入第" << i + 1 << "个数据" << endl;
cin >> p.name >> p.score;
data_l.push_back(p);
}
system("cls");
menu();
string _name;
int _score;
bool flag = false;
//op
while (1) {
cin >> a;
switch (a) {
case 1://add
cout << "输入序号在其后添加" << endl;
cin >> b;
if (b > size(data_l)) {
cout << "错误的数字" << endl;
system("pause");
system("cls");
menu();
break;
}
cout << "输入添加的姓名和分数" << endl;
cin >> t.name >> t.score;
data_l.push_back(t);
system("pause");
system("cls");
menu();
continue;
case 2://del
cout << "输入将删除数据的姓名" << endl;
cin >> _name;
for (it = data_l.begin(); it != data_l.end(); ++it) {
if (_name == (*it).name)
{
flag = true;
data_l.erase(it);
}
if (flag == false) {
cout << "错误的名字" << endl;
system("pause");
system("cls");
menu();
break;
}
it = data_l.begin();
}
system("cls");
menu();
continue;
case 3://change
cout << "输入将修改的分数学生的名字" << endl;
cin >> _name;
cout << "输入修改后的数据" << endl;
cin >> _score;
for (it=data_l.begin(); it != data_l.end(); ++it)
{
if (_name == (*it).name)
{
flag = true;
(*it).score = _score;
}
if (flag == false) {
cout << "错误的名字" << endl;
system("pause");
system("cls");
menu();
break;
}
}
system("cls");
menu();
continue;
case 4://find
cout << "输入0打印当前数据,输入1进行查找" << endl;
cin >> b;
switch (b) {
case 0:
cout << "姓名\t分数" << endl;
for (it = data_l.begin(); it != data_l.end(); ++it) {
cout << (*it).name << "\t" << (*it).score << endl;
}
system("pause");
system("cls");
menu();
continue;
case 1:
cout << "输入查找的学生姓名" << endl;
cin >> _name;
for (it = data_l.begin(); it != data_l.end(); it++) {
if((*it).name==_name)
{
flag = true;
cout << "查找的数据为:" << (*it).name << "\t" << (*it).score << endl;
}
if (flag == false) {
cout << "错误的名字" << endl;
system("pause");
system("cls");
menu();
break;
}
}
system("pause");
system("cls");
menu();
continue;
}
case 5://return
return;
case 6://quit
exit(0);
}
}
}