c++ - 交联 vector 中的段错误

标签 c++

我正在尝试创建一个学生 vector 和一个大学 vector ,其中每个学生都有一份他正在申请的大学列表,每所大学都有一份已申请的学生列表。 但是当我运行代码时,出现“Segmentation fault: 11”。你能帮我理解我哪里做错了吗?

struct Student;

struct Univercity
{
    std::string name;
    int vacancies;
    std::vector<Student *> students;

    Univercity(std::string name,
               int vacancies)
        : name(name),
          vacancies(vacancies) {}
};

struct Student {
    std::string name, surname;
    int d, m, y;
    int points;
    std::vector<Univercity *> univercities;

    Student(std::string name,
            std::string surname,
            int d, int m, int y, int points,
            std::vector<Univercity *> univercities)
        : name(name),
          surname(surname),
          d(d), m(m), y(y),
          univercities(univercities) {}
};

void input(std::vector <Student> *students,
           std::vector <Univercity> *univercities) {
    int n;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string name;
        int vacancies;
        std::cin >> name >> vacancies;
        univercities->push_back(Univercity(name, vacancies));
    }
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string name, surname;
        int d, m, y, points;
        int k;
        std::vector<Univercity *> applications;
        std::string uni_name;
        std::cin >> name >> surname >> d >> m >> y >> points >> k;
        for (int j = 0; j < k; j++) {
            std::cin >> uni_name;
            for (auto u : *univercities) {
                if (u.name == uni_name) {
                    applications.push_back(&u);
                    break;
                }
            }
        }
        students->push_back(Student(name, surname, d, m, y,
                                    points, applications));
    }

}

int main() {
    std::vector <Univercity> univercities;
    std::vector <Student> students;
    input(&students, &univercities);
    for (auto s : students) {
        std::cout << s.surname << " " << s.univercities.size() << "\n";
        for (auto u : s.univercities) {
            std::cout << u->name << " " << u->vacancies << "\n";
        }
    }
}

示例输入:

3
MSU 1
HSE 2
MIPT 100
5
Ivan Ivanov 1 1 1900 100 2 MSU HSE
Petr Petrov 2 1 1900 90 2 MSU HSE
Alexander Sidorov 3 1 1900 110 2 MIPT HSE
Ivan Petrov 3 1 1900 100 3 HSE MSU MIPT
Petr Ivanov 4 1 1900 80 1 HSE

最佳答案

当你阅读你的输入时:

for (auto u : *univercities) {
    if (u.name == uni_name) {
        applications.push_back(&u);
        break;
    }
}

在基于范围的 for 循环中,u 是 vector 中元素的拷贝。因此,一旦您离开循环,您插入 vector 的地址就无效了。使用

for (auto& u : *univercities) { //...

使用引用并避免复制。

关于c++ - 交联 vector 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39687763/

相关文章:

c++ - 在工作线程创建新 GUI 元素的地方使用 Qt

c++ - 无法写入注册表

python - 在带有防火墙的 Windows 上手动安装 pyodbc

c++ - 查找一对 N 是素数优化

c++ - 为什么在移动赋值中需要 std::variant 成为 valueless_by_exception?

c++ - 如何检查 string::append() 是否会失败

c++ - 多类型分配器设计

c++ - 为什么当对象包含在 C++ 中的另一个对象中时,复制构造函数被调用两次?

c++ - OPENCV可以使用视频捕捉设备访问模拟摄像机吗

c++ - 在C++中实现光栅化和深度缓冲