在 for 循环中填充后向量大小 returns 0
Vector size returns 0 after being populated in a for loop
我一直在开发一个小型 C++ 应用程序,该应用程序与讲师和学生根据 class 他们 teach/take 匹配在一起。
每个讲师都有一个学生向量。当一名学生与讲师教授的课程相同 class 时,我们会将同一名学生添加到同一位讲师。
我有 2 个 for 循环遍历所有讲师和学生,然后比较两者的 classes,如果它们匹配,则将该学生添加到讲师。
循环后,我遍历所有讲师并获取每个讲师的大小。但是 returns 返回 0,它应该返回 1。(因为一个学生匹配每个讲师的 class)。
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;
}
}
您到处都在使用价值观。这意味着副本。
您的第一个更改是使用 references 进行迭代。例如:
for (Lecturer& lecturer : lecturers)
// ^
其他不涉及最终是您的实际错误的注释:
Each Lecturer has a vector of students.
为什么?每个讲师应该有一个(n 无序)set 个学生。学生不会同时存在多次。此外,它们没有固有的相关顺序。实际上,讲师们所教授的每一门课程都需要这样一套。
此外,您为什么假设每个学生只选了一个 class? (看源码)啊!现在我懂了。
- 你的"students"不是学生,真的是学生的课业记录
- 您的"lecturers"不是讲师,它们是带有讲师信息的课程记录。
非常混乱。请解决这个问题。如果您使用适当的 terms/names 和适当的数据结构,您很可能会自己找出错误。
I have 2 for loops that loop through all lecturers and students and then compares the classes of both and if they match, add that student to the lecturer.
两个循环?在这种情况下,你自己写它们太深了——就像你在重新发明轮子一样!用 std::copy_if
的调用替换内部循环。这也可能有助于找到错误。
我一直在开发一个小型 C++ 应用程序,该应用程序与讲师和学生根据 class 他们 teach/take 匹配在一起。
每个讲师都有一个学生向量。当一名学生与讲师教授的课程相同 class 时,我们会将同一名学生添加到同一位讲师。
我有 2 个 for 循环遍历所有讲师和学生,然后比较两者的 classes,如果它们匹配,则将该学生添加到讲师。
循环后,我遍历所有讲师并获取每个讲师的大小。但是 returns 返回 0,它应该返回 1。(因为一个学生匹配每个讲师的 class)。
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;
}
}
您到处都在使用价值观。这意味着副本。
您的第一个更改是使用 references 进行迭代。例如:
for (Lecturer& lecturer : lecturers)
// ^
其他不涉及最终是您的实际错误的注释:
Each Lecturer has a vector of students.
为什么?每个讲师应该有一个(n 无序)set 个学生。学生不会同时存在多次。此外,它们没有固有的相关顺序。实际上,讲师们所教授的每一门课程都需要这样一套。
此外,您为什么假设每个学生只选了一个 class? (看源码)啊!现在我懂了。
- 你的"students"不是学生,真的是学生的课业记录
- 您的"lecturers"不是讲师,它们是带有讲师信息的课程记录。
非常混乱。请解决这个问题。如果您使用适当的 terms/names 和适当的数据结构,您很可能会自己找出错误。
I have 2 for loops that loop through all lecturers and students and then compares the classes of both and if they match, add that student to the lecturer.
两个循环?在这种情况下,你自己写它们太深了——就像你在重新发明轮子一样!用 std::copy_if
的调用替换内部循环。这也可能有助于找到错误。