e*****r 发帖数: 144 | 1 int main()
{
string s = "A B C "; // output is A B C C
// If string s = "A B C", then the output would be A B C
istringstream iss(s);
string temp;
while (!iss.eof())
{
iss >> temp;
cout << temp << endl;
}
}
So why an extra white space in s matters? | z*y 发帖数: 1311 | 2 First, when eof bit is set
iss >> temp;
cout << temp << endl;
will always return the last element read, which in this case is C. This may
or may not be a bug depending how you look at it.
When it comes to the last white space, it returns immediately with element C.
But the eof bit is not set yet. Then it calls "iss >> temp" again. Nothing
is read, the last element C is returned (second time), the eof bit is set,
and the while loop quits.
If the last character is C, it returns C and at the same time sets the eof
bit. So the while quits and no extra C is printed. | g*****y 发帖数: 7271 | 3 应该是读过了以后(iss >> temp)再check是否eof吧?
【在 e*****r 的大作中提到】 : int main() : { : string s = "A B C "; // output is A B C C : // If string s = "A B C", then the output would be A B C : istringstream iss(s); : string temp; : while (!iss.eof()) : { : iss >> temp; : cout << temp << endl;
| m********5 发帖数: 17667 | 4 why you use iss.eof()?
The common way to do this is directly check after iss>>temp, because their
can be fail, bad, whitespace, or eof. If either fail/bad signal is True, the
temp is not valid.
###
void test(){
string s = "A B C "; // output is A B C C
// If string s = "A B C", then the output would be A B C
istringstream iss(s);
string temp;
int _i = 0;
while (!iss.eof())
{
cout << _i << " iss stat[good,bad,fail,eof]: [" << iss.good() << iss
.bad() <
iss >> temp;
cout << _i<< " iss stat after: [" << iss.good() << iss.bad() <
fail() <
_i++;
}
}
###>>>
0 iss stat[good,bad,fail,eof]: [1000] 0 iss stat after: [1000] temp: A
1 iss stat[good,bad,fail,eof]: [1000] 1 iss stat after: [1000] temp: B
2 iss stat[good,bad,fail,eof]: [1000] 2 iss stat after: [1000] temp: C
3 iss stat[good,bad,fail,eof]: [1000] 3 iss stat after: [0011] temp: C
###
We can see the last reading is not valid since the state is fail.
if the s does not have the whitespace:
###>>>
0 iss stat[good,bad,fail,eof]: [1000] 0 iss stat after: [1000] temp: A
1 iss stat[good,bad,fail,eof]: [1000] 1 iss stat after: [1000] temp: B
2 iss stat[good,bad,fail,eof]: [1000] 2 iss stat after: [0001] temp: C
###
We can see the last reading is not fail/bad just eof meaning the last
reading is OK but no further reading is needed.
Here is the common way:
void test2(){
string s = "A B C "; // output is A B C C
// If string s = "A B C", then the output would be A B C
istringstream iss(s);
string temp;
int _i = 0;
while (iss>>temp)
{
cout << _i<< " iss stat after: [" << iss.good() << iss.bad() <
fail() <
_i++;
}
}
test2:
0 iss stat after: [1000] temp: A
1 iss stat after: [1000] temp: B
2 iss stat after: [1000] temp: C
【在 e*****r 的大作中提到】 : int main() : { : string s = "A B C "; // output is A B C C : // If string s = "A B C", then the output would be A B C : istringstream iss(s); : string temp; : while (!iss.eof()) : { : iss >> temp; : cout << temp << endl;
| e*****r 发帖数: 144 | 5 多谢。
你讲述的很详细。
the
【在 m********5 的大作中提到】 : why you use iss.eof()? : The common way to do this is directly check after iss>>temp, because their : can be fail, bad, whitespace, or eof. If either fail/bad signal is True, the : temp is not valid. : ### : void test(){ : string s = "A B C "; // output is A B C C : // If string s = "A B C", then the output would be A B C : istringstream iss(s); : string temp;
|
|