如何使用相同的stringstream
对象标记两个不同的字符串?如何使用相同的stringstream对象标记两个不同的字符串?
我尝试下面的代码,但它不工作:
using namespace std;
void check()
{
stringstream s("This is a test");
string token;
while (s>>token)
{
cout<< token <<'\n';
}
s.str("hello world");
while (s>> token)
{
cout<< token <<'\n';
}
}
int main()
{
check();
int z;
cin>>z;
}
当您在第一个while循环中用'>>'到达流的末尾时,会设置eof位。这就是为什么你需要调用clear()来重置流,如答案中所示。 – 2013-03-13 03:36:53