尝试循环检查 Nullptr C++

Trying to loop check for Nullptr C++

我想弄清楚如何交替检查数组中的指针当前是否为 NULL 并循环。目前如果数组那部分的内容被删除,循环时会报错。

此数组已初始化:

Student *classRosterArray[5] = { nullptr, nullptr, nullptr, nullptr, nullptr };

循环

void Roster::add(string studentID, string firstName, string lastName, string email, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeProgram)
{
    int courseDaysin[3] = { daysInCourse1, daysInCourse2, daysInCourse3 };

    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i] == nullptr) {
            if (degreeProgram == NETWORKING) {
                classRosterArray[i] = new NetworkStudent(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram);
            }
            else if (degreeProgram == SECURITY) {
                classRosterArray[i] = new SecurityStudent(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram);
            }
            else if (degreeProgram == SOFTWARE) {
                classRosterArray[i] = new SoftwareStudent(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram);
            }
            else {
                classRosterArray[i] = new Student(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram);
            }

            break;//stop 
        }
    }
}

删除时:

void Roster::remove(string studentID)
{
    bool studentRemoved = false;
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i] != nullptr && classRosterArray[i]->fetchStudentId() == studentID) {
            classRosterArray[i] = nullptr;
            studentRemoved = true;
            break;
        }
    }

    if (studentRemoved == false) {
        cout << "ERROR: Student ID '" << studentID << "' was not found.";
    }
}

已编辑以添加以下代码片段 以及之前建议的修改 我现在使用的是 Map 而不是原始数组,我应该如何更改以下内容。感谢你们到目前为止的帮助!

void Roster::printAll()
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        classRosterArray[i]->print();
    }
}

void Roster::printByDegreeProgram(int degreeProgram)
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i]->fetchDegreeProgram() == degreeProgram) {
            classRosterArray[i]->print();
        }
    }
}

void Roster::printDaysInCourse(string studentID)
{
    float avg = 0;
    int max = 3;
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i] != nullptr && classRosterArray[i]->fetchStudentId() == studentID) {
            int *daysInCourse = classRosterArray[i]->fetchDaysInCourse();
            for (int x = 0; x < max; x++) {
                avg += daysInCourse[x];
            }

            cout << "Student " << classRosterArray[i]->fetchStudentId() << "'s average number of days in each course is." << (avg / max) << "\n";
            break;
        }
    }
}

void Roster::printInvalidEmails()
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        string email = classRosterArray[email]->fetchEmail();
        bool isValid = false;

        size_t found = email.find("@");
        if (found != string::npos) {
            found = email.find(".");
            if (found != string::npos) {
                found = email.find(" ");
                if (found == string::npos) {
                    isValid = true;
                }
            }
        }

        if (!isValid) {
            cout << email << " is not a valid email address \n";
        }
    }
}

您的代码有问题:

  • 您使用的是普通 C 数组,它们更难使用且更容易破坏
  • 在您的删除方法中,您没有使用 "delete" 删除您使用 "new" 创建的对象,因此您泄露了每个删除的学生。
  • 如果您有超过 5 个学生,您的添加方法将失败且没有任何错误报告
  • 在一个真实的程序中,当你有大量的学生为每个 "add" 或 "remove" 操作迭代所有这些时,性能会受到很大影响。

在现代 C++ 中应该这样写:

map<string, shared_ptr<Student> > classRosterArray;

void Roster::add(string studentID, string firstName, string lastName, string email, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeProgram)
{
  int courseDaysin[3] = { daysInCourse1, daysInCourse2, daysInCourse3 };
  switch (degreeProgram) {
     case NETWORKING:
         classRosterArray[studentID] = std::shared_ptr<Student>(new NetworkStudent(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram));
         break;
     case SECURITY:
         classRosterArray[studentID] = shared_ptr<Student>(new SecurityStudent(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram));
         break;
         /* [...] */
     default:
         classRosterArray[studentID] = shared_ptr<Student>(new Student(age, courseDaysin, studentID, email, firstName, lastName, degreeProgram));
  }
}

void Roster::remove(string studentID)
{
    auto it = classRosterArray.find(studentID);
    if (it != classRosterArray.end())
        classRosterArray.erase(it);
    else
        cout << "ERROR: Student ID '" << studentID << "' was not found.";
}