【问题标题】:Vector size returns 0 after being populated in a for loop向量大小在 for 循环中填充后返回 0
【发布时间】:2019-10-26 18:24:50
【问题描述】:

我一直在开发一个小型 c++ 应用程序,该应用程序与讲师和学生根据他们教授/参加的课程匹配在一起。

每个讲师都有一个学生向量。当学生与讲师教授相同的课程时,我们会将同一学生添加到同一讲师。

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

循环之后,我遍历所有讲师并获取每个讲师的大小。但返回 0,它应该返回 1。(因为一个学生与每个讲师的班级相匹配)。

讲师.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();
}

学生.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


    【解决方案1】:

    您在任何地方都在使用值。这意味着副本。

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

    for (Lecturer& lecturer : lecturers)
    //           ^
    

    【讨论】:

    • 非常感谢,它解决了我的问题,来自Java背景,这真的很难记住,在迭代时加上引用符号'&'。再次感谢您!
    • @YM_coding:你经常想写for (auto&amp; lectuer : lecturers)for (const auto&amp; lecturer : lecturers)
    • @einpoklum 见仁见智。
    • @LightnessRacesinOrbit:是的,但它强调你选择的是 no ref、ref 或 const ref(通常)。
    【解决方案2】:

    其他不涉及最终的实际错误的注释:

    每个 Lecturer 都有一个学生向量。

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

    另外,你为什么假设每个学生都上一门课? (查看源代码)啊!现在我懂了。

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

    非常混乱。请解决这个问题。如果您使用适当的术语/名称和适当的数据结构,您就不太可能自己找出错误。

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

    两个循环?在这种情况下自己编写它们太深了——就像你在重新发明轮子一样!用std::copy_if 调用替换内部循环。这也可能有助于定位错误。

    【讨论】:

    • 感谢您的回答。是的,你是对的,我的学生与讲师并不是真正的学生,但我会把他们变成真正的学生,只是还没有(进展缓慢)。关于“学生不会多次并行存在”这到底是什么意思?使用矢量有什么问题?非常感谢!
    • @YM_coding:如果您不想将同一个学生选课记录插入同一个“讲师”两次。
    • 其中一些可能是很好的建议,但都不是问题的答案。
    • 使用集合而不是向量的建议在这种规模下可能是错误的教训。
    • 关于两个循环“太多”的讨论是荒谬的,并且具有误导性。这是一个 O(n*m) 操作。没有两种[原文如此]方法。是否将其中一个循环隐藏在标准库调用后面并不重要。你的话听起来像是一个 O(n) 操作。它不能。
    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2016-08-15
    相关资源
    最近更新 更多