我正在从文件中读取结构,我想将它们添加到结构向量中。 下面是它的外观和工作方式:向量的结构:添加元素C++
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'? – 2013-04-06 11:40:19
什么是文件格式?每个学生的名字都由两个字组成吗? – 2013-04-06 11:42:09
请注意,您没有结构向量,您有一个指针向量。所有这些指向相同的对象... – juanchopanza 2013-04-06 11:43:57