exgdreamer 2023-03-12 17:22 采纳率: 50%
浏览 30
已结题

关于#c++list#的问题:当我初始化完数据后进行查找,输入1打印第二个数据,打印数据也消失了

操作时数据删除
当我初始化完数据(比如说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);
        }
    }
}

  • 写回答

3条回答 默认 最新

  • QWQ.qwq 2023-03-12 17:48
    关注

    说一下题

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月17日
  • 修改了问题 3月12日
  • 创建了问题 3月12日