while(getline(myReadFile, temp, ':')) 执行一次迭代太多导致向量越界
while(getline(myReadFile, temp, ':')) executing one iteration too many causing out of bounds on vector
我有一个关于std::readline(istream,string,delimiter)的问题。我正在尝试读入一个文件,将数据存储在一个结构中,并将其添加到映射中。我已经开始工作了。然而,我的while循环迭代一个循环太多,导致我的向量中没有存储任何数据,从而导致越界断言失败。
我已经阅读了readline上的每个堆栈溢出问题,似乎没有一个问题让我知道为什么会发生这种行为。也许这里有人能启发我。
if (myReadFile.is_open()) {
while(getline(myReadFile, temp, ':')){//loops through and splits the line one delimiter at a time
stringVector.push_back(temp);//add to vector
ItemInfo tempItem = ItemInfo(stringVector[1], stod(stringVector[2]), stod(stringVector[3]));//create the struct
catalog.insert(pair<string,ItemInfo>(stringVector[0], tempItem));//add to map
stringVector.clear();
}
}
myReadFile.close();
感谢的帮助
当前代码应该在第一次迭代时中断。
假设您使用一个空向量开始,那么就可以读取tmp
中文件的第一行。然后将tmp推回到向量中,仍然可以。矢量正好包含索引0的一个元素。
但在下一行中,您将尝试使用向量中索引为1、2和3的元素,该向量只包含一个元素=>索引超出限制异常保证。
相关文章:
- 在一次迭代中从 txt 文件中读取多行
- 多流迭代器 c++
- 将多个 for 循环组合成单个迭代器
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- GetDIBits 在屏幕捕获函数中多次迭代后返回零
- modify() 在迭代期间提升多索引键
- 具有C++迭代器参数的多功能函数
- 在C++中,是否可以在单个循环中顺序迭代多个迭代器
- 如何在多次迭代后停止for循环
- Rand()大小写开关在多次迭代后运行到无穷大
- 如何使equal_range迭代器在 Boost 多索引中按不同的索引排序?
- 在多个 STL 对对象上创建迭代器
- 静态包装库的多态迭代器,而不向用户公开库
- 将多个非原始递归调用转换为迭代解决方案
- 前向迭代器多通道保证的优势
- 虽然循环迭代了C++中的太多元素
- 前向迭代器与多个绑定相结合的迭代速度太快
- C :在多迭代器中的Next()和PRAV()的运行时间
- while(getline(myReadFile, temp, ':')) 执行一次迭代太多导致向量越界
- 迭代器,循环导致seg故障!为什么循环太多