C++ 从 istream 读取直到换行(但不是空格)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:28:04  来源:igfitidea点击:

C++ read from istream until newline (but not whitespace)

c++iostream

提问by StephQ

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

我有一个 std::istream 指的是矩阵数据,例如:

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

在我阅读 2.0 和 istream 之后,...something... 部分评估为 false 应该在 3.0 以便下一个

istream >> x;

should set x equal to 3.0.

应该设置 x 等于 3.0。

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

你将如何达到这个结果?我猜是 while 条件

Thank you very much in advance for your help!

非常感谢您的帮助!

采纳答案by tzaman

Use the peekmethod to check the next character:

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

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

回答by Clifford

Read the lines into a std::string using std::getline(), then assign the string to a std::istringstreamobject, and extract the data from that rather than directly from istream.

使用std::getline()将行读入 std::string ,然后将该字符串分配给std::istringstream对象,并从中提取数据,而不是直接从 istream 中提取数据。

回答by David Rodríguez - dribeas

std::vector<double> vec;
{
   std::string line;
   std::getline( ifile, line );
   std::istringstream is(line);
   std::copy( std::istream_iterator<double>(is), std::istream_iterator<double>(),
              std::back_inserter(vec) );
}
std::cout << "Input has " << vec.size() << " columns." << std::endl;
std::cout << "Read values are: ";
std::copy( vec.begin(), vec.end(), 
           std::ostream_iterator<double>( std::cout, " " ) );
std::cout << std::endl;

回答by MKroehnert

You can use std::istream::peek()to check if the next character is a newline. See this entryin the cplusplus.com reference.

您可以使用std::istream::peek()来检查下一个字符是否为换行符。见该条目在cplusplus.com参考。

回答by Manohar Reddy Poreddy

I had similar problem
Input is as below:

我有类似的问题
输入如下:

1 2
3 4 5

The 1st two were N1 and N2
Then there is a newline
then elements 3 4 5, i dont know how many these will be.

前两个是 N1 和 N2
然后有一个换行符,
然后是元素 3 4 5,我不知道这些会有多少。

// read N1 & N2 using cin
int N1, N2;
cin >> N1;
cin >> N2;

// skip the new line which is after N2 (i.e; 2 value in 1st line)
cin.ignore(numeric_limits<streamsize>::max(), '\n');

// now read 3 4 5 elements
int ele;
// 2nd EOF condition may required,
//    depending on if you dont have last new-line, and it is end of file.
while ((cin_.peek() != '\n') && (cin_.peek() != EOF)) {
  cin >> ele;
  // do something with ele
}

This worked perfect for me.

这对我来说很完美。

回答by Messa

Read the number, then read one character to see if it's newline.

读取数字,然后读取一个字符以查看它是否为换行符。