(cin)如何评估? [英] How is (cin) evaluated?
问题描述
在Bjarne Stroustrup的使用C ++的编程原理和实践(第六版,2012年11月), if(cin)
和 if(!cin)
在p.148中引入并在p.178上认真使用。 while(cin)
在p.183上引入,并在p.201上认真使用。
In Bjarne Stroustrup's Programming Principles and Practice Using C++ (Sixth Printing, November 2012), if (cin)
and if (!cin)
are introduced on p.148 and used in earnest on p.178. while (cin)
is introduced on p.183 and used in earnest on p.201.
感觉我不完全明白这些构造如何工作,所以我正在探索他们。
However, I feel I don't fully understand how these constructs work, so I'm exploring them.
如果我编译并运行:
int main()
{
int i = 0 ;
while (cin) {
cout << "> ";
cin >> i ;
cout << i << '\n';
}
}
我得到类似的结果:
$ ./spike_001
> 42
42
> foo
0
$
- 进入foo显然导致
i
设置为0
? - 为什么输入foo导致
cin
被设置为false
? - Why is it that entering "foo" apparently causes
i
to be set to0
? - Why is it that entering "foo" causes
cin
to be set tofalse
?
或者,如果我运行并编译:
Alternatively, if I run and compile this:
int main()
{
int i = 0 ;
while (true) {
cout << "> ";
cin >> i ;
cout << i << '\n';
}
}
我得到类似的结果:
$ ./spike_001
> 42
42
> foo
> 0
> 0
...
用户输入的最后一部分是 foo
。输入之后,> 0
The last part of user input here is foo
. After that is entered, the line > 0
is printed to stdout repeatedly by the program, until it is stopped with Ctrl+C.
- 再次,通过程序重复打印到<为什么进入foo显然导致
i
被设置为0
? - 为什么在
foo 下的while循环的下一次迭代时,不会提示用户输入
i
code>已输入?
- Again, why is it that entering "foo" apparently causes
i
to be set to0
? - Why is it that the user is not prompted for a new value for
i
on the next iteration of the while loop afterfoo
was entered?
这是使用 g ++(Ubuntu / Linaro 4.6.3-1ubuntu5 )4.6.3
。
我很抱歉这么长的问题,但我想这一切都归结为评估?
I'm sorry for such a long question, but I think it all boils down to, "How is cin evaluated?"
推荐答案
好吧, std :: cin
正确地 std :: basic_ios
)有 运算符bool
在请求时将 std :: cin
对象隐式转换为 bool
(例如, code> if 评估)。
Well, std::cin
(or more precisely std::basic_ios
) has an operator bool
to implicitly convert the std::cin
object to a bool
when requested (for example in your if
evaluation).
语义如下:
检查流是否没有错误。如果流没有错误并且可以进行I / O操作,则返回 true
。具体来说,返回!fail()
。
Checks whether the stream has no errors. Returns
true
if the stream has no errors and is ready for I/O operations. Specifically, returns!fail()
.
为什么输入foo显然导致我被设置为0?
Why is it that entering "foo" apparently causes i to be set to 0?
输入foo显然会将i设置为0?
Again, why is it that entering "foo" apparently causes i to be set to 0?
因为 operator>
Because operator>>
on an int
expects an integer in the string.
从 //en.cppreference.com/w/cpp/io/basic_istream/operator_gtgtrel =nofollow> cppreference
:
如果提取失败(例如,如果一个字母输入到一个数字的预期位置),值保持不变,并设置failbit。
If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set.
为什么输入foo会导致cin设置为false? p>
Why is it that entering "foo" causes cin to be set to false?
因为失败位被设置,因此引导 fail()
返回 true
。
Because the fail bit is set, therefore leading fail()
to return true
.
在输入foo之后,在while循环的下一次迭代中,不会提示用户输入新的值?
Why is it that the user is not prompted for a new value for i on the next iteration of the while loop after foo was entered?
std :: cin
设置了失败位,因此无法获取输入,只打印 i
0
。
That is because std::cin
has the fail bit set, therefore it fails to get the input and just prints the old value of i
which is 0
.
这篇关于(cin)如何评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!