类中成员变量的数量不确定?C++

Indefinite Number Of Member Variables In A Class? C++

本文关键字:不确定 C++ 成员 变量      更新时间:2023-10-16

对于我的 GroupPicker 程序,我需要允许class group为每个随机生成的数字提供无限数量的插槽,这些数字分配给每个输入的学生。代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>
using namespace std;
class student
{
    int m_studentNumber;
public:
    string nameFirst;
    string nameLast;
    string nameFull;
    int getStudentNumber() { return m_studentNumber; }
    void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};
class group
{
public:
};
ostream& operator<<(ostream& os, const student& s)
{
     return os << s.nameFirst << ' ' << s.nameLast;
}
student typeName()
{
    student bar;
    cout << "Type in a student's first name: ";
    cin >> bar.nameFirst;
    cout << "Type in that student's last name: ";
    cin >> bar.nameLast;
    cout << "n";
    bar.nameFull = bar.nameFirst + " " + bar.nameLast;
    return bar;
}
void displayStudents(student listOfStudents[50], int studentHeadCount)
{
    for (int i = 0; i < studentHeadCount; i++)
    {
        cout << listOfStudents[i].nameFull << endl;
        cout << listOfStudents[i].getStudentNumber() << endl;
        cout << "n";
    }
}
void options()
{
    cout << "Select what you want to do:n";
    cout << "1) Exit applicationn";
    cout << "2) Enter a Studentn";
    cout << "3) Display Studentsn";
    cout << "4) Display Groupsn";
    cout << "5) Output groups as text filen";
    cout << "n";
}
int main()
{
    student allStudents[50]; // Having 50 students alone is ridiculous
    bool endProg = 0;
    int maxStudents;
    int studentsPerGroup;
    int optionSelect;
    int studentHeadCount = 0;
    int remainder;
    int numberOfGroups;
    cout << "GroupPicker 1.0n";    
    cout << "How many students are in the class?n" << "(Note: You cannot have more than 50 in this program)n";
    cin >> maxStudents;
    if (maxStudents > 50)
    {
        cerr << "Too many students! Exiting program...n";
        system("PAUSE");
        exit(1);
    }
    cout << "How many students per group?n";
    cin >> studentsPerGroup;
    if (studentsPerGroup >= maxStudents)
    {
        cerr << "You're kidding, right?n" << "Exiting program...n";
        system("PAUSE");
        exit(1);
    }
    while (endProg == 0) {
        options();
        cin >> optionSelect;
        switch (optionSelect) {
            case 1:
                endProg = 1;
                break;
            case 2:
            {
                if (studentHeadCount == maxStudents)
                {
                    cerr << "You can't enter more than " << maxStudents << " studentsn";
                }
                else
                {
                    allStudents[studentHeadCount] = typeName();
                    allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
                    studentHeadCount++;
                }
                break;
            }
            case 3:
                cout << "Current list of students:nn";
                displayStudents(allStudents, studentHeadCount);
                break;
            case 4:
            {
                if (studentHeadCount < studentsPerGroup || studentHeadCount < maxStudents)
                {
                    cerr << "Invalid group parameters.n" << "Returning to main menu...n";
                    break;
                }
                else
                {
                cout << "Here are the groups:n";
                numberOfGroups = maxStudents / studentsPerGroup;
                }
            }
            case 5:
            {
                cout << "Saving groups to file...n";
                ofstream studentGroups;
                studentGroups.open("studentGroups.txt");
            }
        }
    }
}

我如何在小组班级中拥有无限数量的变量,具体取决于每个小组有多少学生?

您不能有"无限"的数字,因为您将受到可用内存的限制。 但是,可以有一个在编译时未指定的数字。 将学生数组更改为学生向量:

std::vector<student> allStudents;

您似乎正在寻找一种数据结构,例如 std::vector<student>

你实际上不需要每个小组中有无限数量的学生。你真正需要的是每个小组中无限数量的学生。为此,您可以在group类中使用任何标准容器,例如std::vector<student>std::list<student>,也可以替换group类。