本文介绍了C ++从istream读取,直到换行符(但不是空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std :: istream指向矩阵数据,像:

  0.0 1.0 2.0 
3.0 4.0 5.0



现在,为了评估列数,我想有一些代码:

  std :: vector< double> vec; 
double x;
while((... something ...)&&(istream>> x))
{
vec.push_back(x);
}
//这里vec应包含0.0,1.0和2.0

... ... ... ...部分在我读取2.0之后计算为false,istream在该点应该在3.0,以便下一个

  istream>> X; 

应设置x等于3.0。



你将如何实现这个结果?

解决方案



非常感谢您的帮助! / div>

使用方法来检查下一个字符:

  while((istream.peek()!='\\\
')& ;&(istream>> x))


I have a std::istream which refers to matrix data, something like:

0.0 1.0 2.0
3.0 4.0 5.0

Now, in order to assess the number of columns I would like to have some code like:

std::vector<double> vec;
double x;
while( (...something...) && (istream >> x) )
{
    vec.push_back(x); 
}
//Here vec should contain 0.0, 1.0 and 2.0

where the ...something... part evaluates to false after I read 2.0 and istream at the point should be at 3.0 so that the next

istream >> x;

should set x equal to 3.0.

How would you achieve this result? I guess that the while condition

Thank you very much in advance for your help!

解决方案

Use the peek method to check the next character:

while ((istream.peek()!='\n') && (istream>>x))

这篇关于C ++从istream读取,直到换行符(但不是空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 07:42