我正在從文件中讀取結構,我想將它們添加到結構向量中。 下面是它的外觀和工作方式:向量的結構:添加元素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