cin 在一个 while 循环中
我写了一个简单的代码:
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 << "
";
}
}
我们知道 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
,根据参考 这里,这个操作符返回一个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
.当标准输出捕捉到 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 << "
";
}
}
如果你使用的是像 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
代表这个stream
的状态(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.
相关文章