简体   繁体   English

如何确定另一个结构内部的结构数组的长度

[英]How to determine the length of an array of structs inside another struct

I am trying to write a function that acts on a member of a struct that is in an array of structs that is a member of another struct :). 我正在尝试编写一个函数,该函数作用于结构体数组中的结构体成员,该结构体是另一个结构体的成员:)。 The first thing I need to do is find out the length of the array of structs, but I keep getting the error expression must be class type. 我需要做的第一件事是找出结构体数组的长度,但是我不断得到的错误expression must be class type.

What would be the appropriate way for me to get the length of this array? 对我来说,获取此数组长度的合适方法是什么? (PS. The function has to take the Student structure as an argument.) (PS。此函数必须将Student结构作为参数。)

The structs: 结构:

struct Class
{
    string title; 
    int units;
    char grade;

};
struct Student
{
    string name;
    double gpa;
    Class classes[500];
};

My function looks something like this: 我的函数看起来像这样:

void gpaCalculate (Student s)
{
    int size = s.classes.size() ;
    //Lots of awesome code
}

The code 编码

struct Student
{
    string name;
    double gpa;
    Class classes[500];
};

indicates that a Student can attend to maximum 500 classes, and that this is some school assignment where you are expected/required to use raw arrays, instead of the otherwise much more appropriate std::vector . 表示一个Student最多可以参加500个课程,这是一些学校的作业,要求/要求您使用原始数组,而不是其他更合适的std::vector

First, the magic number 500 should be made a constant, like so: 首先,应将魔术数 500设为常数,如下所示:

int const maxClasses = 500;

Then in each Student object you need to store the actual number of classes this student attends to: 然后,在每个Student对象中,您需要存储该学生参加的实际班级数量:

struct Student
{
    string name;
    double gpa;
    int nClasses;
    Class classes[maxClasses];

    Student()
        : name(), gpa( 0.0 ), nClasses( 0 ), classes()
    {}
};

The constructor ensures that gpa and nClasses will be zeroed. 构造函数确保gpanClasses将被清零。

Since they're of built-in types that's otherwise not guaranteed (per the principle that you don't pay for what you don't use, so it's your decision). 由于它们是内置类型,因此无法保证(根据您不为不使用的商品付款的原则,这是您的决定)。

In answer to the question "How do I determine the length of an array?", use (sizeof(array) / sizeof(*array)) , as in 在回答“如何确定数组的长度?”这一问题时,请使用(sizeof(array) / sizeof(*array)) ,如

void gpaCalculate (Student s)
{
    int size = (sizeof s.classes) / sizeof(*s.classes);
    //Lots of awesome code
}

Note: this will not work with variable-length array parameters. 注意:这不适用于可变长度数组参数。 In answer to the question "How should I store a list of items in C++", generally you should use a vector. 在回答“我应该如何在C ++中存储项目列表”这一问题时,通常应该使用向量。 Vectors tend to be easier to work with than arrays. 向量往往比数组更易于使用。 For example, vectors unlike arrays can change size. 例如,与数组不同的向量可以改变大小。

What if you changed the structures to C++ classes? 如果将结构更改为C ++类怎么办? Then you could create a function to access the size of the array of Classes. 然后,您可以创建一个函数来访问类数组的大小。 you could easily call the gpaCalulate function as a function of the Class class. 您可以轻松地将gpaCalulate函数作为Class类的函数来调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM