结构体的向量:添加元素的c++
Vector of structs: adding elements 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;
}
我该怎么做才能把我的结构体添加到vector中,并正常打印出来(这个打印实际上只是一个检查,如果结构体被正确加载到vector中)?
下面是用现代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);
// ...
}
你的错误是使用指针
std::vector<Student> students;
Student s;
while(theFile >> s.ID >> s.name >> s.surname >> s.points)
{
students.push_back(s);
}
现在可以运行了。
问题是你反复使用相同的指针。所以你最终得到一个向量,所有的指针都指向同一个对象。它将具有最后一个学生读入的值
当简单的选择是正确的时候,选择复杂的选择似乎是一个相当普遍的初学者特征,所以我很想知道你为什么选择使用指针。
因为你想在vector中存储student指针而不是student指针,
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,并且每次循环都是一次又一次地为它读取。
应该在每个循环中分配一个新的student,然后读入新分配的内存。
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
}
当你不再需要矢量时,不要忘记删除每个学生
你的代码不工作,因为你有一个学生对象,每次都覆盖它的成员。解决方案是每次创建一个新的Student对象,并将指向它的指针传递给vector:
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
{
// ...
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- 擦除while循环中迭代的元素