如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
How do I make a C++ program that reads info in an array of strings and then sorts it into a class?
更具体地说,我需要采用格式化为
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);
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何设置一个范围来提取我想要获得的信息
- 在std::thread中,joinable()然后join()线程安全吗
- C++:如何读取分离变量,然后读取向量
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么我的递归函数按降序打印,然后按升序打印?
- 为什么 gcovr 会生成空覆盖率统计信息?
- 等待整个 omp 块完成,然后再调用第二个函数
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将人类可读日期转换为毫秒,然后再转换回来,而不会丢失信息
- 从控制台保存信息,然后重新打开时,信息仍然存在