简体   繁体   English

如何在C ++中将结构数据存储在数组中?

[英]How to store struct data in an array in C++?

I have this program for school it gets data about a student, does a few calculations and stores the data in a struct which is returned by the input function. 我在学校有这个程序,它获取有关学生的数据,进行一些计算并将数据存储在由输入函数返回的结构中。

Right now I've only got it working for one student, but I need to be able to store and output data for more than one student. 现在我只为一个学生工作,但是我需要能够存储和输出一个以上学生的数据。

"Right now I've only got it working for one student, but I need to be able to store and output data for multiple students." “现在我只为一个学生工作,但是我需要能够存储和输出多个学生的数据。”

Use std::vector 使用std::vector

int n; //No. of student

std::vector<studentType> vec;
studentType s;

for(size_t i =0; i<n ;++i)
{
  s = input();
  vec.push_back(s);
}

And then you can access 然后您可以访问

vec[i].studentID ; // etc, for ith student

On another note, void main is not legal C++, use int main 另外, void main不是合法的C ++,请使用int main

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

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