向量大小在for循环中填充后返回0

问题描述 投票:0回答:2

我一直在研究一个小型的c ++应用程序,该应用程序与讲师和学生根据他们所授课/参加的课程而配对在一起。

[每位讲师都有一群学生。当学生上课与讲师讲授的课程相同时,我们会将同一名学生添加到该讲师中。

我有2个for循环,遍历所有讲师和学生,然后比较两者的班级,如果它们匹配,则将该学生添加到讲师中。

循环后,我循环浏览所有讲师,并获取每个讲师的人数。但是返回0,它应该返回1。(因为一位学生与每个讲师的班级相匹配)。

Lecturer.h

#pragma once
#include <iostream>
#include "Person.h"
#include "Student.h"
#include <vector>
#include <string>

using namespace std;

class Lecturer : public Person {

public:
    // Lecturer(string department, string specialization, string name, int age, char gender)
    //     : department(department), specialization(specialization), name(name), age(age), gender(gender) {}

    Lecturer() { }

    Lecturer(string department, string specialization, string name, int age, char gender, string uniClass){
        this->department = department;
        this->specialization =specialization;
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->uniClass = uniClass;
    }

    // Class Methods
    void addStudent(Student student);

    // Setter Methods
    void setDepartment(string dprt);
    void setSpecialization(string splz);
    void setName(string nme);
    void setAge(int ag);
    void setGender(char g);

    // Getter Methods
    string getDepartment();
    string getSpecialization();
    string getUniClass();

    int getStudentsSize();
    vector<Student> getStudents();

private:
    string department;
    string specialization;
    vector<Student> students;
    string uniClass;
};

void Lecturer::addStudent(Student student)
{
    cout << student.getName() << endl;
    students.push_back(student);
}

int Lecturer::getStudentsSize()
{
    return students.size();
}

Student.h

#pragma once
#include <iostream>
#include "Person.h"
#include <string>

using namespace std;

class Student : public Person {

public:
    // Student(string major, string minor, int id, string name, int age, char gender)
    //     : major(major), minor(minor), id(id), name(name), age(age), gender(gender) {}

    Student() { }

    Student(string major, string minor, int id, string name, int age, char gender, string uniClass){
        this->major = major;
        this->minor = minor;
        this->id = id;
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->uniClass = uniClass;
    }
    // Setter Methods
    void setMajor(string mjr);
    void setMinor(string mnr);
    void setId(int _id);
    void setName(string nme);
    void setAge(int ag);
    void setGender(char g);

    // Getter Methods
    string getMajor();
    string getMinor();
    int getId();
    string getUniClass();
    string getName();

private:
    string major;
    string minor;
    int id;
    string uniClass;
};

string Student::getUniClass()
{
    return uniClass;
}

main.cpp

#include <iostream>
#include <string>
#include "Person.h"
#include "Lecturer.h"
#include "Student.h"

int main()
{

    vector<Lecturer> lecturers;
    lecturers.push_back(Lecturer("Computing", "Advanced Programming", "John", 40, 'm', "AB101"));
    lecturers.push_back(Lecturer("Business", "Finance", "Dave", 42, 'm', "AB102"));
    lecturers.push_back(Lecturer("Science", "Physics", "Bill", 46, 'm', "AB103"));

    vector<Student> students;
    students.push_back(Student("Computer Science", "Maths", 123, "Mike", 20, 'm', "AB101"));
    students.push_back(Student("Business", "Economics", 142, "Jane", 21, 'f', "AB102"));
    students.push_back(Student("Engineering", "Physics", 151, "Mary", 19, 'f', "AB103"));

    for(Lecturer lecturer : lecturers)
    {
        for(Student student : students)
        {
        //cout << "Name: " << student.getUniClass() << endl;
        if (lecturer.getUniClass().compare(student.getUniClass()) == 0)
            {
                // ADDING A STUDENT THAT MATCHES THE CLASS
                lecturer.addStudent(student);
            }
        }
    }

    for(Lecturer lecturer : lecturers)
    {
        // EACH LECTURER'S STUDENTS SIZE IS 0 HERE (SHOULD BE 1)
        cout << lecturer.getStudentsSize() << endl;
    }
}
c++ c++11 vector stdvector c++-standard-library
2个回答
2
投票

您在各处使用值。这意味着副本。

您的第一个更改是使用references进行迭代。例如:

for (Lecturer& lecturer : lecturers)
//           ^

1
投票

每个讲师都有一个学生矢量。

为什么?每个讲师应有一个(n个无序)set学生。学生不会并行存在多次。而且,它们没有固有且相关的顺序。实际上,讲师为他们所教的每门课程都需要这样一套。

而且,为什么要假设每个学生都上一堂课? (查看源代码)啊!现在我懂了。

  • 您的“学生”不是学生,他们实际上是学生的学习记录。
  • 您的“讲师”不是讲师,他们实际上是包含讲师信息的课程记录。

非常令人困惑。请解决此问题。如果使用适当的术语/名称和适当的数据结构,则不太可能会自己找出错误。

我有2个for循环,遍历所有讲师和学生,然后比较两者的班级,如果它们匹配,则将该学生添加到讲师中。

两个循环?太多了!最多只有一个循环。将内部循环替换为std::copy_if的调用。这也可能有助于找到该错误。

© www.soinside.com 2019 - 2024. All rights reserved.