【发布时间】:2016-05-23 19:25:59
【问题描述】:
我正在尝试编写自己的词汇表并为我的小兄弟进行测试,但是当我想将文件中的数据读入两个数组时遇到问题 - 首先是英语单词,第二个是波兰语单词。文件看起来很像
黑色 - czarny
红色 - czerwony 等。
还有我的功能:
void VOC::readout()
{
fstream file;
VOC *arr = new VOC;
string line;
file.open("slowka.txt");
if(file.good())
{
int i=0;
while(!file.eof())
{
getline(file, line);
size_t pos = line.find(" - ");
int position = static_cast<int>(pos);
file>>arr[i].en;
file>>arr[i].pl;
++i;
}
}
}
我认为在第一个数组中插入一行直到函数找到“-”是个好主意,然后将其余行插入到第二个数组中,但我有一些问题。有人可以帮助我吗?我知道我可以通过使用 std::vector 来解决它,但我想通过使用数组来解决它。
【问题讨论】:
-
arr指向 一个 单个VOC对象并且不是数组。由于您不知道有多少,std::vector是您的最佳选择。 -
你在用
int position = static_cast<int>(pos);做什么? -
我认为将 size_t 与 int 进行比较会导致溢出,我错了吗?
-
与其使用 2 个数组,不如使用包含两个单词的结构中的一个
std::vector。