结构体的向量:添加元素的c++

Vector of structs: adding elements C++

本文关键字:元素 c++ 添加 向量 结构体      更新时间:2023-10-16

我正在从文件中读取我的结构体,我想将它们添加到结构体向量中。下面是它的外观和工作原理:

    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
{
    // ...
}