[英]cin inside a while loop
I have written a simple code:我写了一个简单的代码:
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b) //Note the cin inside while loop
{
cout << a << b << "\n";
}
}
We know that while
loop functions only when the expression evaluates true
( 1
) or false
( 0
).我们知道
while
循环仅在表达式计算为true
( 1
) 或false
( 0
) 时起作用。 How come cin
is evaluating true
and false
. cin
如何评估true
和false
。
Also how is while loop running when I am entering a number and stops when I enter something non-digit ?当我输入数字时while循环如何运行并在我输入非数字内容时停止? How is it evaluating true and false?
它是如何评价真假的?
When you writes cin >> a
, you are actually using the std::istream::operator>>
, according to the reference here , this operator returns an istream&
object reference , and took the right hand variable (reference) as its argument.当你写
cin >> a
,你实际上是在使用std::istream::operator>>
,根据这里的引用,这个操作符返回一个istream&
对象引用,并把右手边的变量(引用)作为它的参数。 This is how you can chain it like: cin >> a >> b
.这就是你可以像这样链接它的方式:
cin >> a >> b
。
To see this cin >> a >> b
chain another way, when break down, it is this two steps:换个角度看这个
cin >> a >> b
链,分解的时候就是这两个步骤:
cin >> a
returns some intermediate value, let's say it is x
.cin >> a
返回一些中间值,假设它是x
。 (You can actually try auto x = cin >> a
. auto x = cin >> a
。(cin >> a) >> b
, when we use this intermediate value x
, we could write it as x >> b
.(cin >> a) >> b
,当我们使用这个中间值x
,我们可以把它写成x >> b
。 So what the hell is this x
?那么这个
x
到底是什么? x
here stays a same position as the cin
, it is an object of istream&
type. x
在这里与cin
保持相同的位置,它是istream&
类型的对象。
Therefore, when you talk about true
or false
, you are actually talking about whether this returned istream&
reference, refer to an object, whether it is true
or false
.因此,当您谈论
true
或false
,您实际上是在谈论 this 返回的istream&
引用,引用一个对象,它是true
还是false
。 It would be false
when the standard output catch an EOF sign (like when you type Ctrl-C in unix like system, or when you have read to the end of a file).当标准输出捕捉到 EOF 符号时(例如在类似 unix 的系统中键入 Ctrl-C 时,或者当您读到文件末尾时),这将是
false
。
Your code, therefore, could be expanded as因此,您的代码可以扩展为
#include <iostream>
using namespace std;
int main()
{
int a, b;
auto x = cin >> a >> b
while (x)
{
cout << a << b << "\n";
}
}
If you are using an IDE like Visual Studio, you could point your mouse at the variable x
, it would prompt you x
's type, and that would be an istream&
.如果您使用的是像 Visual Studio 这样的 IDE,您可以将鼠标指向变量
x
,它会提示您x
的类型,这将是一个istream&
。
Also, thanks to Bob__, this istream&
class could be convert to an ios::operator bool
class, as is written here , whether it is true
or false
represents the state( ios_base::iostate
) of this stream
, it therfore,另外,多亏了 Bob__,这个
istream&
类可以被转换为一个ios::operator bool
类,正如这里所写的,无论它是true
还是false
代表了这个stream
的状态( ios_base::iostate
),因此,
makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such as
while(stream >> value) {...}
orwhile(getline(stream, string)){...}
.使得使用流和函数返回
while(stream >> value) {...}
引用作为循环条件成为可能,从而产生惯用的 C++ 输入循环,例如while(stream >> value) {...}
或while(getline(stream, string)){...}
. Such loops execute the loop's body only if the input operation succeeded.只有当输入操作成功时,这样的循环才会执行循环体。
To further your understanding, you should read the operator (overloading) chapter in your textbook.为了进一步理解,您应该阅读教科书中的运算符(重载)章节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.