为什么循环 for stringstream 两次给我最后一个词
Why does loop for stringstream give me the last word twice
我试图在 stringstream
的帮助下逐字循环字符串,这是我的代码:
string str = "hello world";
stringstream ss(str);
string word;
while (ss)
{
ss >> word;
cout << word << endl;
}
然而,我得到的结果如下:
hello
world
world
为什么我得到了两次world
?
while (ss)
看到 ss
还没有遇到问题,所以它运行循环体。 (这就是当您将 ss
用作布尔值时发生的情况)
ss >> word;
读取 "hello"
cout << word << endl;
打印 "hello"
while (ss)
看到ss
还没有遇到问题,所以再次运行循环体。
ss >> word;
读取 "world"
cout << word << endl;
打印 "world"
while (ss)
看到ss
还没有遇到问题,所以再次运行循环体。
ss >> word;
看到没有更多的数据,所以失败。 word
不变,仍然包含"world"
cout << word << endl;
打印 "world"
while (ss)
发现 ss
遇到问题并停止循环。
您需要检查在读完这个词后是否停止循环。例如,使用:
while (true)
{
ss >> word;
if (!ss)
break;
cout << word << endl;
}
或简称:
while (ss >> word)
{
cout << word << endl;
}
使用此代码片段:
while (ss) { ... }
您正在检查 string stream
的状态。如果它包含有效数据,循环将继续。这就是为什么您两次看到最后一个词的原因...
1st iteration of loop:
while ( ss ) { // checks if there is valid data in ss and for errors (true)
ss >> word; // ss inserts "hello" into word.
cout << word << endl; // cout prints "hello" to the console.
}
2nd iteration of loop:
while ( ss ) { // checks again if there is valid data (true)
ss >> word; // ss inserts "world" into word.
cout << word << endl; // cout prints "world" to the console.
}
3rd iteration of loop:
while ( ss ) { // checks again if there is valid data (true)
ss >> word; // ss already contains "world", may optimize or copy over...
cout << word << endl; // cout prints "world" to the console.
}
4th iteration of loop:
while ( ss ) { // ss encountered end of stream (false) exits loop.
ss >> word; // nothing inserted as loop has exited.
cout << word << endl; // nothing printed as loop has exited.
}
与其尝试将 stringstream
用作循环条件,不如尝试使用从 stringstream
中提取数据并将其转化为条件变量的过程。
while( ss >> word ) {
cout << word << endl;
}
1st iteration of loop:
while ( ss >> word ) { // checks if ss inserted data into word
// ss inserts "hello" (true)
cout << word << endl; // cout prints "hello" to the console.
}
2nd iteration of loop:
while ( ss >> word ) { // checks again if ss inserted data into word
// ss inserts "world" into word (true)
cout << word << endl; // cout prints "world" to the console.
}
3rd iteration of loop:
while ( ss >> word ) { // checks again if ss inserted data into word
// ss fails to insert data (false) loop exits here
cout << word << endl; // nothing printed as loop exited
}
我试图在 stringstream
的帮助下逐字循环字符串,这是我的代码:
string str = "hello world";
stringstream ss(str);
string word;
while (ss)
{
ss >> word;
cout << word << endl;
}
然而,我得到的结果如下:
hello
world
world
为什么我得到了两次world
?
while (ss)
看到ss
还没有遇到问题,所以它运行循环体。 (这就是当您将ss
用作布尔值时发生的情况)ss >> word;
读取 "hello"cout << word << endl;
打印 "hello"while (ss)
看到ss
还没有遇到问题,所以再次运行循环体。ss >> word;
读取 "world"cout << word << endl;
打印 "world"while (ss)
看到ss
还没有遇到问题,所以再次运行循环体。ss >> word;
看到没有更多的数据,所以失败。word
不变,仍然包含"world"cout << word << endl;
打印 "world"while (ss)
发现ss
遇到问题并停止循环。
您需要检查在读完这个词后是否停止循环。例如,使用:
while (true)
{
ss >> word;
if (!ss)
break;
cout << word << endl;
}
或简称:
while (ss >> word)
{
cout << word << endl;
}
使用此代码片段:
while (ss) { ... }
您正在检查 string stream
的状态。如果它包含有效数据,循环将继续。这就是为什么您两次看到最后一个词的原因...
1st iteration of loop:
while ( ss ) { // checks if there is valid data in ss and for errors (true) ss >> word; // ss inserts "hello" into word. cout << word << endl; // cout prints "hello" to the console. }
2nd iteration of loop:
while ( ss ) { // checks again if there is valid data (true) ss >> word; // ss inserts "world" into word. cout << word << endl; // cout prints "world" to the console. }
3rd iteration of loop:
while ( ss ) { // checks again if there is valid data (true) ss >> word; // ss already contains "world", may optimize or copy over... cout << word << endl; // cout prints "world" to the console. }
4th iteration of loop:
while ( ss ) { // ss encountered end of stream (false) exits loop. ss >> word; // nothing inserted as loop has exited. cout << word << endl; // nothing printed as loop has exited. }
与其尝试将 stringstream
用作循环条件,不如尝试使用从 stringstream
中提取数据并将其转化为条件变量的过程。
while( ss >> word ) {
cout << word << endl;
}
1st iteration of loop:
while ( ss >> word ) { // checks if ss inserted data into word // ss inserts "hello" (true) cout << word << endl; // cout prints "hello" to the console. }
2nd iteration of loop:
while ( ss >> word ) { // checks again if ss inserted data into word // ss inserts "world" into word (true) cout << word << endl; // cout prints "world" to the console. }
3rd iteration of loop:
while ( ss >> word ) { // checks again if ss inserted data into word // ss fails to insert data (false) loop exits here cout << word << endl; // nothing printed as loop exited }