C ++ struct问题

时间:2016-10-15 22:06:38

标签: c++ struct

我用google搜索与struct有关,我能够看到它们是如何被使用的;但是,我无法清楚地弄清楚其中的一些。

假设我有两个结构

struct Student {
    int age;
    int height;
};

struct School {
    Student information;
};

让我们说我想处理信息学校[i]。学生[j] .age或基于输入文件的高度。

int main() {
    int school_number = 20;
    int student_number = 50;

    School school[school_number-1]; // is this the correct way to create it? since [0] is the first school

    for (int i=0; i < school_number; i++) {
        for (int j=0; j < student_number; j++) {
            getline(file, line); // let's say the line has student's age and height.
            istringstream c(line); 
            c >> school[i].information[j].age >> school[i].information[j].height;
        }
    }
}

我认为这样可以完成这项工作,但是我找不到'operator []'(操作数类型是'Student'和'int')编译错误。

我错过了什么?

当它只是学生时,

Student info[student_number-1];

for (int i=0; i < student_number; i++) {
        getline(file, line);
        istringstream c(line);
        c >> information[i].age >> information[i].height;
}

这个没有问题,但我仍然不确定我需要为2个结构做什么,其中一个调用另一个结构。

还有一个问题,

在我搜索的时候,我看到很多

School *id = new School[school_number-1];
像这样的事情。这与

有什么不同
School school[school_number-1];

这一个?

我看到一个指针,所以我很确定它有所作为,但根据它们的使用方式,它们看起来非常相似。

编辑:我已经尝试了一点,但在这种情况下仍然不确定如何使用向量。

对于上述情况,

int main() {
    vector[Student] student;

    int school_number = 20;
    int student_number = 50;

    School school[school_number-1]; // is this the correct way to create it? since [0] is the first school

    for (int i=0; i < school_number; i++) {
        for (int j=0; j < student_number; j++) {
            getline(file, line); // let's say the line has student's age and height.
            istringstream c(line); 
            c >> school[i].information[j].age >> school[i].information[j].height;
        }
    }
}

如果我打电话

vector[Student] student;

如何修改行

c >> school[i].information[j].age >> school[i].information[j].height;

我刚刚创建的变量学生?

4 个答案:

答案 0 :(得分:2)

如果要声明数组,则必须具有const大小。你可能想要的是std::vector<Student>

答案 1 :(得分:0)

你宣布上学的方式只有一名学生。你可能想要一个学生矢量而不是你学校的一个学生。

声明数组时,放在括号内的数字是数组的长度。如果数组的长度为n,则使用数组时,您可以访问0n-1的元素。当声明时,您应该将n放在括号中。

指针和数组是非常接近的概念,在许多情况下可以互换。如果您的C经验很少,那么坚持使用数组更安全。

答案 2 :(得分:0)

struct School {
    Student information;
};

在此代码struct School中只包含一个Student对象,因此school[i].information[j]失败。 (它尝试在未定义的operator[]对象Student上调用school[i].information。)

School school[school_number];

(C风格的数组)无效C ++,因为school_number是一个变量,而C风格的数组只能有一定数量的项。在现代C ++中也不推荐使用它们。

std::array<School, 20> school;

是创建20个School个对象的数组的推荐方法。

因为在此代码中school_number具有固定值,所以可以将其定义为

constexpr int school_number = 20;

然后它可以与std::array或C风格的数组一起使用。 此外,数组需要school_number项,而不是school_number-1。然而,school_number-1是其最后一项的索引(因为它们在索引时从0开始计算)。

对于具有可变数量项目的数组,可以使用std::vector

std::vector<School> school(school_number);

或者,使用

创建一个空矢量
std::vector<School> school;

然后使用

School个对象插入其中
school.push_back(sch);

所以程序可以这样写:

struct Student {
    int age;
    int height;
};

struct School {
    std::vector<Student> information; // School has array of students
};

int main() {
    int school_number = 20;
    int student_number = 50;

    std::vector<School> schools;
    for(int i = 0; i < school_number; i++) {
        School school;  // first create School object, and then insert it into schools array
        for(int j = 0; j < student_number; j++) {
            Student student;  // same for Student object
            getline(file, line);
            istringstream c(line); 
            c >> student.age >> student.height;
            school.information.push_back(student);
        }
        schools.push_back(school);
    }
}
School *id = new School[school_number-1];

创建一个school_number-1数组,并返回指向它的指针。使用

后,需要手动释放指针
delete[] id;

它返回一个指针,因为内存在堆上分配。

std::vector<School>会自动执行此操作,并且还可以在从数组中插入/删除项目时增加/减少已分配内存的大小。

答案 3 :(得分:-1)

School *id = new School[school_number-1];

School school[school_number-1];

第一个在堆内存上声明,第二个在堆栈上声明。您必须在完成后立即删除第一个,以避免内存泄漏。

相关问题