C ++:while循环内的cin [英] C++ : 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循环如何运行,而当我输入非数字内容时,停止了吗??
Also how is while loop running when I am entering a number and stops when I enter something non-digit? How is it evaluating true and false?
推荐答案
当您编写 cin>>时,它如何评估真假? a
,实际上根据参考文献 std :: istream :: operator>> /www.cplusplus.com/reference/istream/istream/operator%3E%3E/ rel = nofollow noreferrer>在此,此运营商返回 istream&
对象引用,并以右侧变量(引用)作为参数。您可以通过以下方式链接它: cin>>一个>> b
。
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>> a
返回一些中间值,假设它是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
。都将是 false
当标准输出捕获到EOF符号时(例如,当您在类似unix的系统中键入Ctrl-C时,或者已读取到文件末尾时)。
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).
因此,您的代码可以使用命名空间std; $ b $扩展为
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
类,如在此,无论是 true
还是 false
表示此流
的状态( ios_base :: iostate
),因此,
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.
这篇关于C ++:while循环内的cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!