我正在从文件中读取我的结构体,并希望将它们添加到结构体向量中。 以下是代码的样子和工作方式:
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;
}
我该怎么做,才能将我的结构体添加到向量中,并正常地打印它们(这个打印只是一个检查,确保结构体已正确加载到向量中)?
typedef
? - Kerrek SB