如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?

How do I make a C++ program that reads info in an array of strings and then sorts it into a class?

本文关键字:然后 信息 排序 数组 读取 创建 何创建 一个 程序 C++ 字符串      更新时间:2023-10-16

更具体地说,我需要采用格式化为

const string studentData[4] =
{"A1,John,Smith,John1989@gmail.com,20,30,35,40,SECURITY",
"A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
"A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY"}

并将它们全部放入一个类的数组中,其中格式化为

class student {
public:
void getter();
void setter();
private:
string ID;
string firstName;
string lastName;
string email;
int age;
int courseDays[3];
degree; //degree is an enumerated data type defined in another file.
}

然后,我需要从一个单独的文件中使用一个指针数组,该数组是另一个类的私有元素来执行其他几个功能。主要是,我不知道如何分隔每个字符串中的各个数据点,以便它们可以设置为"学生"类的私有变量的值。

您可以使用流和getline在逗号上拆分字符串:

std::istringstream is{"A1,John,Smith"}; // for example
std::getline(is, ID, ',');
std::getline(is, first_name, ',');
std::getline(is, last_name, '');

对于整数,只需读入数字并丢弃逗号:

is >> age;
is.ignore(1);