指针的向量传递到不同的功能

发布于 2025-02-12 13:25:17 字数 2487 浏览 1 评论 0原文

我正在尝试使用新操作员的使用来练习以创建对象。我在调整代码以将新指针管理到创建的对象时遇到了一些麻烦。

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct StudentRecord {
public:
    StudentRecord(
        string id,
        string firstName,
        string lastName,
        int age,
        string phoneNumber,
        double gpa
    ) {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
        PhoneNumber = phoneNumber;
        Age = age;
        Gpa = gpa;
    }

    void display() {
        cout << "   Student ID: " << Id << endl;
        cout << "   First Name: " << FirstName << endl;
        cout << "    Last Name: " << LastName << endl;
        cout << " Phone Number: " << PhoneNumber << endl;
        cout << "          Age: " << Age << endl;
        cout << "          GPA: " << Gpa << endl;
        cout << endl;
    }

    string Id;
    string FirstName;
    string LastName;
    string PhoneNumber;
    int Age;
    double Gpa;

};

void displayStudents(vector<StudentRecord>& students) {

    for (auto student : students) {
        student.display();
    }
}

int main()
{
    ifstream inputFile;
    inputFile.open("TestFile.csv");
    string line = "";

    vector<StudentRecord> students;
    while (getline(inputFile, line)) {

        stringstream inputString(line);

        //StudentId, Last Name, FirstName, Age, Phone Number, GPA
        string studentId;
        string lastName;
        string firstName;
        int age;
        string phone;
        double gpa;
        string tempString;

        getline(inputString, studentId, ',');
        getline(inputString, lastName, ',');
        getline(inputString, firstName, ',');
        getline(inputString, tempString, ',');
        age = atoi(tempString.c_str());
        getline(inputString, phone, ',');
        getline(inputString, tempString);
        gpa = atof(tempString.c_str());

        students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));
        line = "";
    }

    displayStudents(students);
}

特别是这里有一个问题:

students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));

我知道我需要调整displayStudents函数才能将指针向量引入对象,但我不确定如何执行此操作。

I'm trying to practice with the usage of the new operator to create objects. I'm having some trouble with adjusting my code to manage the new pointers to the objects that I created.

Here's my code:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

struct StudentRecord {
public:
    StudentRecord(
        string id,
        string firstName,
        string lastName,
        int age,
        string phoneNumber,
        double gpa
    ) {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
        PhoneNumber = phoneNumber;
        Age = age;
        Gpa = gpa;
    }

    void display() {
        cout << "   Student ID: " << Id << endl;
        cout << "   First Name: " << FirstName << endl;
        cout << "    Last Name: " << LastName << endl;
        cout << " Phone Number: " << PhoneNumber << endl;
        cout << "          Age: " << Age << endl;
        cout << "          GPA: " << Gpa << endl;
        cout << endl;
    }

    string Id;
    string FirstName;
    string LastName;
    string PhoneNumber;
    int Age;
    double Gpa;

};

void displayStudents(vector<StudentRecord>& students) {

    for (auto student : students) {
        student.display();
    }
}

int main()
{
    ifstream inputFile;
    inputFile.open("TestFile.csv");
    string line = "";

    vector<StudentRecord> students;
    while (getline(inputFile, line)) {

        stringstream inputString(line);

        //StudentId, Last Name, FirstName, Age, Phone Number, GPA
        string studentId;
        string lastName;
        string firstName;
        int age;
        string phone;
        double gpa;
        string tempString;

        getline(inputString, studentId, ',');
        getline(inputString, lastName, ',');
        getline(inputString, firstName, ',');
        getline(inputString, tempString, ',');
        age = atoi(tempString.c_str());
        getline(inputString, phone, ',');
        getline(inputString, tempString);
        gpa = atof(tempString.c_str());

        students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));
        line = "";
    }

    displayStudents(students);
}

Specifically there's an issue here:

students.push_back(new StudentRecord(studentId, lastName,firstName, age, phone, gpa));

I know that I need to adjust the displayStudents function to take in a vector of pointers to the object, but I'm not sure how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2025-02-19 13:25:17

学生是类型vector&lt; studentRecord&gt;,因此,您无需调用new

如果您想练习new,请建议将其更改为vector&lt; studentRecord*&gt;。请注意,建议使用托管指针类型(例如unique_ptrsmart_ptr)而不是“裸”指针。

如果您只想使代码工作,请使用:

students.emplace_back(studentId, lastName,firstName, age, phone, gpa));

无论哪种情况,您都可以在本地生成的字符串上考虑std :: move(),您仅在此处使用,即std :: move(studentId),std :: move(lastName),... 在emplace_back()push_back()的参数中。

students is of type vector<StudentRecord>, therefore, you don't need to call new.

If you wanted to practice new, suggest changing it to vector<StudentRecord*>. Note that it's recommended to use a managed pointer type (e.g. unique_ptr, smart_ptr) instead of 'naked' pointers.

If you'd simply like to make the code work, use:

students.emplace_back(studentId, lastName,firstName, age, phone, gpa));

In either case, you might consider std::move() on locally-generated strings that you only use here, i.e., std::move(studentId), std::move(lastName), ... in the arguments of emplace_back() or push_back().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文