由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 谁能告诉为啥最后为啥输出两个C
相关主题
C++ string to int Problema simple question for C++ class
what's the outcome?which func will be called?
菜鸟请教smart pointer请问一个exception题目
C++一问reverse words, not the Microsoft one!!!
C++11 unique_ptr问题?about new operator
问个很基础的问题:C++的string关于C++中一个Class的大小 (转载)
whitespace 问题C++里面
A try-catch problem in C++两个继承问题
相关话题的讨论汇总
话题: iss话题: temp话题: stat话题: eof话题: string
进入Programming版参与讨论
1 (共1页)
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;

1 (共1页)
进入Programming版参与讨论
相关主题
两个继承问题C++11 unique_ptr问题?
为什么我看不懂下面的code,是不是水平还不够?问个很基础的问题:C++的string
C++ 弱问一个whitespace 问题
C++疑问A try-catch problem in C++
C++ string to int Problema simple question for C++ class
what's the outcome?which func will be called?
菜鸟请教smart pointer请问一个exception题目
C++一问reverse words, not the Microsoft one!!!
相关话题的讨论汇总
话题: iss话题: temp话题: stat话题: eof话题: string