指针的向量传递到不同的功能
我正在尝试使用新操作员的使用来练习以创建对象。我在调整代码以将新指针管理到创建的对象时遇到了一些麻烦。
这是我的代码:
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
学生
是类型vector&lt; studentRecord&gt;
,因此,您无需调用new
。如果您想练习
new
,请建议将其更改为vector&lt; studentRecord*&gt;
。请注意,建议使用托管指针类型(例如unique_ptr
,smart_ptr
)而不是“裸”指针。如果您只想使代码工作,请使用:
无论哪种情况,您都可以在本地生成的字符串上考虑
std :: move()
,您仅在此处使用,即std :: move(studentId),std :: move(lastName),... 在emplace_back()
或push_back()
的参数中。students
is of typevector<StudentRecord>
, therefore, you don't need to callnew
.If you wanted to practice
new
, suggest changing it tovector<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:
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 ofemplace_back()
orpush_back()
.