尝试循环检查Nullptr C ++

时间:2018-11-28 15:19:58

标签: c++ for-loop nullptr

我试图弄清楚如何交替检查数组中的指针当前是否为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";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在问题

  • 您使用的是纯C数组,它们更难使用且更容易破坏
  • 在删除方法中,您没有使用“删除”来删除使用“新建”创建的对象,因此泄漏了每个删除的学生。
  • 如果您的学生人数超过5名,则添加方法将失败,并且没有任何错误报告
  • 在一个真实的程序中,当您有大量的学生对每个“添加”或“删除”操作进行遍历时,会对性能产生很大的影响。

这就是用现代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.";
}