C++ - repeatedly using istringstream(C++ - 反复使用 istringstream)
问题描述
我有一个代码用于读取在线存储的带有浮点数的文件:3.34|2.3409|1.0001|...|1.1|".我想使用 istringstream 来读取它们,但它不像我期望的那样工作:
I have a code for reading files with float numbers on line stored like this: "3.34|2.3409|1.0001|...|1.1|". I would like to read them using istringstream, but it doesn't work as I would expect:
string row;
string strNum;
istringstream separate; // textovy stream pro konverzi
while ( getline(file,row) ) {
separate.str(row); // = HERE is PROBLEM =
while( getline(separate, strNum, '|') ) { // using delimiter
flNum = strToFl(strNum); // my conversion
insertIntoMatrix(i,j,flNum); // some function
j++;
}
i++;
}
在标记点,仅第一次将行复制到单独的流中.在下一次迭代中,它不起作用并且什么也不做.我希望可以在每次迭代中不构建新的 istringstream 对象的情况下使用更多次.
In marked point, row is copied into separate stream only first time. In next iteration it doesn't work and it does nothing. I expected it is possible to be used more times without constructing new istringstream object in every iteration.
推荐答案
将行设置到 istringstream 后...
After setting the row into the istringstream...
separate.str(row);
...通过调用重置它
separate.clear();
这会清除在前一次迭代中或通过设置字符串设置的任何 iostate 标志.http://www.cplusplus.com/reference/iostream/ios/clear/一个>
This clears any iostate flags that are set in the previous iteration or by setting the string. http://www.cplusplus.com/reference/iostream/ios/clear/
这篇关于C++ - 反复使用 istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - 反复使用 istringstream
基础教程推荐
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
