C++ 从文本文件中拆分字符串并将值存储到数组中,而不是向量c++;

C++ 从文本文件中拆分字符串并将值存储到数组中,而不是向量c++;,c++,C++,我有一个文本文件,其中包含一个人的id、姓名、年龄和今天的日期。我知道如何逐行读取字符串并将其存储到vector中并打印出来 age.txt 1:john:23:18-Oct-2013 2:mary:21:18-Oct-2013 3:suzy:20:18-Oct-2013 代码 但我真的不知道如何将它们存储到一个数组中,而不是使用向量并生成它 差不多 personId[] will contain all the id from the output; personName[] will co

我有一个文本文件,其中包含一个人的id、姓名、年龄和今天的日期。我知道如何逐行读取字符串并将其存储到vector中并打印出来

age.txt

1:john:23:18-Oct-2013
2:mary:21:18-Oct-2013
3:suzy:20:18-Oct-2013
代码

但我真的不知道如何将它们存储到一个数组中,而不是使用向量并生成它 差不多

personId[] will contain all the id from the output;
personName[] will contain all the name from the output;
personAge[] will contain all the age from the output;
dateTime[] will contain all the date and time from the output;

请告知。提前感谢

我们首先需要一个结构来捕获您的数据:

struct Data {
   unsigned int id;
   string name;
   unsigned int age;
   time_t time; // might be a C++ data type for this
}
实际上,您将拥有一个
向量


您可以为该(C数组)重载
操作符,您需要提前获得项计数,而这只能通过遍历文件一次获得。您将再次遍历它以进行插入,这一点都不有效。坚持使用向量
你是指std::array还是C array?矢量中的数据在内部存储为数组。因此,在C++11
storeWords.data()
(在C++03
&storeWords[0]
)中,会给您提供原始数组。您想使用数组而不是灵活得多的向量,有什么具体原因吗?可能类似于:while(iss>>personid[i]>>personName[i]>>personage[i]>>personDate[i]){i++;}@arne假设如果用户只想将john的年龄从23岁更改为24岁,我将首先提示他/她想更改姓名的用户,然后它将搜索数组的位置(例如john的姓名存储在personName[0]中),然后允许他更改年龄角色[0]因为john.OP已经知道如何通过向量实现它。他的具体问题是如何使用数组来实现。不,他建议使用4个数组来实现这一点,他的向量用法是string类型。
personId[] will contain all the id from the output;
personName[] will contain all the name from the output;
personAge[] will contain all the age from the output;
dateTime[] will contain all the date and time from the output;
struct Data {
   unsigned int id;
   string name;
   unsigned int age;
   time_t time; // might be a C++ data type for this
}