使用循环为不同的结构变量分配不同的值
Assign different values to different struct variables using loop
我有一个如下所示的考生结构(每个都有 1 个 ID 和他们不同科目的分数):
struct Examinee
{
string id;
float math, literature, physic, chemistry, biology, history, geography, civic_education, natural_science,
social_science, foreign_language;
};
现在我想编写一个函数,从字符串中读取不同的值并将它们分配给考生。字符串看起来像这样(每个信息用逗号分隔):
BD1200001,9,4.0,5.0,10,3.5,7.5,4.25,7.0,7.75,9.25,2.0
这是我目前所做的:
Examinee readExaminee(string line_info) {
//turn line_info to char*
int Line_info_length = line_info.length();
char* info = new char[Line_info_length + 1];
strcpy(info, line_info.c_str());
//create examinee
Examinee examinee;
//read id into examinee by token
char* token = strtok(info, ",");
examinee.id = token;
//read score and assign to subjects
while (token != NULL)
{
float score = strtof(token, NULL);
//assign score to appropriate subject
token = strtok(NULL, ",");
}
delete[] info;
return examinee;
}
问题是:我可以像上面那样在 while 循环中将每个分数分配给每个主题吗?我怎样才能做到这一点?如果不是,手动分配每个分数是唯一的方法吗?
我会更改 Examinee
的设计。这些方面的内容:
struct Examinee
{
enum Subject {kSubjMath, kSubjLiterature, ..., kSubjForeignLanguage, kSubjCount};
string id;
float scores[kSubjCount];
};
这样您就可以循环访问分数,例如
for (int subj = 0; subj < Examinee::kSubjCount; ++subj) {
examinee.scores[subj] = some_score;
}
或访问特定分数 examinee.scores[Examinee::kSubjLiterature]
如果您不能或不愿意更改 Examinee
,您可以在本地进行模拟:
Examinee examinee;
float* scores[] = {&examinee.math, &examinee.literature, ..., &examinee.foreign_language};
for (int subj = 0; subj < std::extent_v<scores>; ++subj) {
*scores[subj] = some_value;
}
我有一个如下所示的考生结构(每个都有 1 个 ID 和他们不同科目的分数):
struct Examinee
{
string id;
float math, literature, physic, chemistry, biology, history, geography, civic_education, natural_science,
social_science, foreign_language;
};
现在我想编写一个函数,从字符串中读取不同的值并将它们分配给考生。字符串看起来像这样(每个信息用逗号分隔):
BD1200001,9,4.0,5.0,10,3.5,7.5,4.25,7.0,7.75,9.25,2.0
这是我目前所做的:
Examinee readExaminee(string line_info) {
//turn line_info to char*
int Line_info_length = line_info.length();
char* info = new char[Line_info_length + 1];
strcpy(info, line_info.c_str());
//create examinee
Examinee examinee;
//read id into examinee by token
char* token = strtok(info, ",");
examinee.id = token;
//read score and assign to subjects
while (token != NULL)
{
float score = strtof(token, NULL);
//assign score to appropriate subject
token = strtok(NULL, ",");
}
delete[] info;
return examinee;
}
问题是:我可以像上面那样在 while 循环中将每个分数分配给每个主题吗?我怎样才能做到这一点?如果不是,手动分配每个分数是唯一的方法吗?
我会更改 Examinee
的设计。这些方面的内容:
struct Examinee
{
enum Subject {kSubjMath, kSubjLiterature, ..., kSubjForeignLanguage, kSubjCount};
string id;
float scores[kSubjCount];
};
这样您就可以循环访问分数,例如
for (int subj = 0; subj < Examinee::kSubjCount; ++subj) {
examinee.scores[subj] = some_score;
}
或访问特定分数 examinee.scores[Examinee::kSubjLiterature]
如果您不能或不愿意更改 Examinee
,您可以在本地进行模拟:
Examinee examinee;
float* scores[] = {&examinee.math, &examinee.literature, ..., &examinee.foreign_language};
for (int subj = 0; subj < std::extent_v<scores>; ++subj) {
*scores[subj] = some_value;
}