我正在从文件中读取我的结构,我想将它们添加到结构的向量中。 以下是它的外观和工作方式:
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;
}
我应该怎么做才能将我的结构添加到矢量中,并将它们正常打印出来(如果结构正确加载到矢量中,这个打印只是一个检查)?
答案 0 :(得分:8)
你的错误是使用指针
std::vector<Student> students;
Student s;
while(theFile >> s.ID >> s.name >> s.surname >> s.points)
{
students.push_back(s);
}
现在它会起作用。
问题是你一遍又一遍地重复使用同一指针。所以你最终会得到一个指向同一个对象的指针向量。哪个会有最后一个学生读的价值。
当更简单的选择正确时,选择复杂的替代方案似乎是一个相当普遍的初学者特征,所以我很想知道你为什么选择使用指针。
答案 1 :(得分:8)
以下是现代C ++中代码的外观:
#include <string>
#include <istream>
#include <vector>
struct Student
{
int ID;
std::string name;
std::string surname;
int points;
Student(int i, std::string n, std::string s, int p)
: ID(i), name(std::move(n)), surname(std::move(s)), points(p) {}
};
std::vector<Student> read_students(std::istream & is)
{
std::vector<Student> result;
std::string name, surname;
int id, points;
while (is >> id >> name >> surname >> points)
{
result.emplace_back(id, name, surname, points);
}
return result;
}
用法:
#include <fstream>
#include <iostream>
int main()
{
std::ifstream infile("test.txt");
auto students = read_students(infile);
// ...
}
答案 2 :(得分:0)
由于您希望在向量中存储指向学生而非学生的指针。
Student* s = new Student();
while(theFile >> s->ID >> s->name >> s->surname >> s->points)
{
students.push_back(s); // here I would like to add this struct s from a file
}
您只分配了一名学生,每次循环时,您都会一次又一次地阅读。
相反,您应该在每个循环上分配一个新学生并读入新分配的内存。
Student* s;
int tmpId, tmpPoints;
string tmpname, tmpsur;
while(theFile >> tmpId >> tmpname >> tmpsur >> tmpPoints)
{
s = new Student();
s->ID = tmpId ;
s->name = tmpname;
s->sur = tmpsur ;
s->points= tmpPoints;
studenti.push_back(s); // here You push a pointer to the newly allocated student
}
else
{
// There is error reading data
}
当你不再需要矢量时,不要忘记删除每个学生。
答案 3 :(得分:0)
您的代码不起作用,因为您有一个Student对象并且每次都覆盖其成员。解决方案是每次创建一个新的Student对象,并将指针传递给向量:
std::vector<Student*> students;
int tmpId, tmpPoints;
string tmpname, tmpsur;
while(theFile >> tmpId >> tmpname >> tmpsur >> tmpPoints)
{
Student* s = new Student();
s->ID = tmpId ;
s->name = tmpname;
s->sur = tmpsur ;
s->points= tmpPoints;
students.push_back(s); // push a pointer to new student object
}
else
{
// ...
}