【问题标题】:How to store an instance of class in a vector?如何将类的实例存储在向量中?
【发布时间】:2022-01-13 18:45:12
【问题描述】:

我为一个有课程和成绩的学生开设了一个班级,该程序一直在要求一个新学生,直到给出的名字停止。为了存储这些实例,我想使用一个向量,但是除了首先为实例创建一个数组然后将它们推回向量中之外,我没有找到任何其他方法来存储它们。 是否可以为一个实例留出空间并在使用后删除Student student 中存储的值以便重复使用?

int i=0;
Student student[20];
vector<Student> students;


cout << "Name?" << endl;
getline(cin,student[i].name);
while((student[i].name) != "stop")
{
    student[i].addcoursegrade();
    students.push_back(student[i]);
    i++;
    cout << "Name?" << endl;
    getline(cin,student[i].name);
    if((student[i].name) == "stop")
        break;

};

我还在类中使用向量来存储课程和成绩的值,因为它们也应该在增长。该类的代码在这里:

class Student {
public:
    string name;

void print() {
    cout << name ;

    for (int i = 0; i < course.size(); i++)
        cout << " - " << course[i] << " - " << grade[i];
    cout<<endl;
}

void addcoursegrade() {
    string coursee;
    string gradee;

    cout << "Course?" << endl;
    getline(cin, coursee);
    course.push_back(coursee);
    while (coursee != "stop") {
        cout << "Grade?" << endl;
        getline(cin, gradee);
        grade.push_back(gradee);
        cout << "Course?" << endl;
        getline(cin, coursee);
        if (coursee != "stop")
            course.push_back(coursee);
        else if(coursee == "stop")
            break;
    }
};

private:
   vector<string> course;
   vector<string> grade;
};

【问题讨论】:

  • 只需使用单个 Student 实例而不是数组。为了清洁,将其范围限定在循环内。 .push_back 复制对象,因此您不需要单独的实例开始。
  • "但是我没有找到任何其他方法来存储它们,而不是" 您究竟是如何尝试“找到”方法来做到这一点的?例如,您是否尝试使用搜索引擎来look for examples of using .push_back
  • 请注意,在这里使用数组几乎会破坏使用vector 的意义。只要输入第 21 个Student,Ka-Blooey!
  • See this。将Student 放入向量中没有问题。因此,获取该代码,添加到其中,然后复制您声称看到的问题。或者只是接受链接上的代码,然后从那里开始工作。

标签: c++ loops class vector instance


【解决方案1】:

与其创建一个数组然后推回,只需保留一个实例并重新分配它:

Student student;
vector<Student> students;

cout << "Name?" << endl;
getline(cin,student.name);
while((student.name) != "stop")
{
    student.addcoursegrade();

    // this line copies the student in the vector
    students.push_back(student);

    // then, reassign the temp student to default values
    student = {};

    cout << "Name?" << endl;
    getline(cin,student.name);
    if((student.name) == "stop")
        break;
};

【讨论】:

  • 这里的关键,也许是 OP 的困惑点,是 vector 存储值类型,所以 student 的内容被复制到 students.push_back(student) 中,留下 student 可用可以重复使用。
  • student = {} 正是我想要的。非常感谢!:D
  • 我的荣幸!另外,请记住 = {} 不会使 student 成为不同的学生,而只是重新分配给默认值。这没关系,因为当您向后推时,它会在向量中复制一份。
【解决方案2】:

有几件事困扰着我:

  • 循环的结构方式,复制getline。当终止输入出现时,我更喜欢带有中断的 while(true) 数组。
  • 不需要 C 样式的数组。 std::vector 就是这样!
  • 课程和成绩的单独数组。相反,我更喜欢同时存储课程和成绩的单一记录
  • 循环中的索引仅用于访问集合中的项目。 (只需使用基于范围的 for 循环)
  • 在需要之前不要创建Student 对象。对字符串输入使用局部变量。

与 C++ 中的任何东西一样,可以做更多的事情来改进它:例如为您的对象添加构造函数、使用现代语法进行初始化、采用移动语义等。但我只是做了最小的更改。

我会这样处理:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct CourseGrade {
    string course;
    string grade;
};

class Student {
public:
    string name;

    void print() {
        cout << name;

        for (auto& courseGrade : courseGrades) {
            cout << " - " << courseGrade.course << " - " << courseGrade.grade;
        }
        cout << endl;
    }

    void addcoursegrades() {
        while (true) {
            cout << "Course?" << endl;
            string course;
            getline(cin, course);
            if (course == "stop") break;

            cout << "Grade?" << endl;
            string grade;
            getline(cin, grade);

            CourseGrade courseGrade;
            courseGrade.course = course;
            courseGrade.grade = grade;
            courseGrades.push_back(courseGrade);
        }
    }

private:
    vector<CourseGrade> courseGrades;
};


int main() {
    vector<Student> students;

    while (true) {
        cout << "Name?" << endl;
        std::string name;
        getline(cin, name);
        if (name == "stop") break;

        Student student;
        student.name = name;
        student.addcoursegrades();
        students.push_back(student);
    };

    for (auto& student : students) {
        student.print();
    }
}

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 2017-04-04
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多