在一个while循环内的cin [英] 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";
}
}
我们知道 while
循环仅在表达式计算为 true
( 1
)或 false
(<代码> 0 ). cin
是如何评估 true
和 false
的.
We know that while
loop functions only when the expression evaluates true
(1
) or false
(0
).
How come cin
is evaluating true
and false
.
当我输入数字时,while循环如何运行,当我输入非数字的内容时,while循环如何运行?它如何评估是非?
推荐答案
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. This is how you can chain it like: cin >> a >> b
.
要查看此 cin>>一个>>b
链的另一种方式,当发生故障时,需要执行以下两个步骤:
To see this cin >> a >> b
chain another way, when break down, it is this two steps:
- 第一步,
cin>>一个
返回一些中间值,假设它是x
.(您实际上可以尝试auto x = cin>> a
. - 第二步,您正在执行
(cin>> a)>>.b
,当我们使用中间值x
时,我们可以将其写为x>>.b
.
- First step,
cin >> a
returns some intermediate value, let's say it isx
. (You can actually tryauto x = cin >> a
. - Second step, you are doing
(cin >> a) >> b
, when we use this intermediate valuex
, we could write it asx >> b
.
那么这 x
到底是什么? x
在这里与 cin
保持相同的位置,它是 istream&
类型的对象.
So what the hell is this x
? x
here stays a same position as the cin
, it is an object of istream&
type.
因此,当您谈论 true
或 false
时,实际上是在谈论此返回的 istream&
引用是否指向对象.,无论是 true
还是 false
.当标准输出捕获到EOF符号时(例如,当您在类似unix的系统中键入Ctrl-C时,或者当您读取到文件的末尾时),将为 false
.
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
. 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).
因此,您的代码可以扩展为
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";
}
}
如果使用的是类似Visual Studio的IDE,则可以将鼠标指向变量 x
,它会提示您 x
的类型,即 istream&
.
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&
.
此外,由于Bob__,该 istream&
类可以转换为 ios :: operator bool
类,如
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,
使使用流和返回对流的引用作为循环条件的函数成为可能,从而导致惯用的C ++输入循环,例如
while(stream>> value){...}
或while(getline(stream,string)){...}
.仅当输入操作成功时,此类循环才会执行循环的主体.
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)){...}
. 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.
这篇关于在一个while循环内的cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!