提取一行中的条目数 [英] extract number of entries in a line
问题描述
什么是优雅的方式(使用std :: stream')在格式化文本文件的给定行中提取
空格分隔条目的数量?
例如:取一个文件的这一部分,
1 2.78 4 5 -0.003< tab> 7.1d5
9 40< tab> 2.e5 -10
....
代码将返回第1行6,下一行4等...
- slyi
What is an elegant way (using std::stream''s) to extract number of
white-space separated entries in a given line of a formatted text file?
e.g.: Take this section of a file,
1 2.78 4 5 -0.003 <tab> 7.1d5
9 40 <tab> 2.e5 -10
....
and the code will return 6 for 1st line, 4 for next line, etc...
- slyi
推荐答案
levent写道:
什么是一种优雅的方式(使用std :: stream')在格式化文本文件的给定行中提取
空白分隔条目的数量?
例如:采取此部分一个文件,
1 2.78 4 5 -0.003< tab> 7.1d5
9 40< tab> 2.e5 -10
...
代码将返回第1行6,下一行4等...
What is an elegant way (using std::stream''s) to extract number of
white-space separated entries in a given line of a formatted text file?
e.g.: Take this section of a file,
1 2.78 4 5 -0.003 <tab> 7.1d5
9 40 <tab> 2.e5 -10
...
and the code will return 6 for 1st line, 4 for next line, etc...
>
使用''std :: getline''读取行。
从刚读过的字符串中定义''std :: ostringstream''。
将字段作为std :: string对象读取,直到字符串流结束。
V
Read the line using ''std::getline''.
Define ''std::ostringstream'' from the string you just read.
Read fields as ''std::string'' objects until the end of the string stream.
V
Victor,你的意思是说std :: istringstream吗?
字符串行;
istringstream iss(行);
while(! iss.eof())
{
iss>>字;
cout<<单词<< endl;
iss.ignore(256,'''');
}
Victor, Did you mean to say std::istringstream?
string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, '' '');
}
Victor,我忘了剪切和粘贴右大括号。
字符串行;
istringstream iss(行);
而(!iss.eof())
{
iss>>字;
cout<<单词<< endl;
iss.ignore(256,'''');
}
Victor, I forgot to cut and paste the closing brace.
string line;
istringstream iss(line);
while (!iss.eof())
{
iss >> word;
cout << word << endl;
iss.ignore(256, '' '');
}
这篇关于提取一行中的条目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!