我正在从文件中读取我的结构,我想将它们添加到结构向量中。以下是它的外观和工作方式:
typedef struct
{
int ID;
string name;
string surname;
int points;
}
Student;
int main()
{
ifstream theFile("test.txt");
std::vector<Student*> students;
Student* s = new Student();
while(theFile >> s->ID >> s->name >> s->surname >> s->points)
{
studenti.push_back(s); // here I would like to add this struct s from a file
}
// here I want to print each struct's values on the screen, but the output is always ONLY last struct N times, and not all of them, each only once
std::vector<Student*>::const_iterator it;
for(it = students.begin(); it != students.end(); it+=1)
{
std::cout << (*it)->ID <<" " << (*it)->name << " " << (*it)->surname <<" " << (*it)->points <<endl;
}
我应该怎么做才能将结构添加到向量中,然后正常打印出来(如果结构正确加载到向量中,此打印实际上只是一个检查)?