【发布时间】:2011-03-27 00:10:06
【问题描述】:
这是一个使用字符串流的示例程序。目标是接受来自用户的行(标准输入)并将每个单词打印在单独的行中。
int main()
{
std::istringstream currentline;
std::string eachword;
std::string line;
// Accept line from the standard input till EOF is reached
while ( std::getline(std::cin,line) )
{
currentline.str(line); // Convert the input to stringstream
while ( currentline >> eachword ) // Convert from the entire line to individual word
{
std::cout << eachword << std::endl;
}
currentline.clear();
}
return 0;
}
我想知道,有没有办法,我可以避免中间字符串变量(对象),行并将用户输入直接存储到当前行(istringstream对象)。
注意:
我知道,下面的解决方案已经有了。
while ( std::cin >> eachword)
{
std::cout << eachword << std::endl;
}
【问题讨论】:
-
为什么不直接使用第二种解决方案?
-
尼尔,这就是我打算做的。