1. <tfoot id='DJR8L'></tfoot>
    2. <small id='DJR8L'></small><noframes id='DJR8L'>

      • <bdo id='DJR8L'></bdo><ul id='DJR8L'></ul>
      <legend id='DJR8L'><style id='DJR8L'><dir id='DJR8L'><q id='DJR8L'></q></dir></style></legend>
      <i id='DJR8L'><tr id='DJR8L'><dt id='DJR8L'><q id='DJR8L'><span id='DJR8L'><b id='DJR8L'><form id='DJR8L'><ins id='DJR8L'></ins><ul id='DJR8L'></ul><sub id='DJR8L'></sub></form><legend id='DJR8L'></legend><bdo id='DJR8L'><pre id='DJR8L'><center id='DJR8L'></center></pre></bdo></b><th id='DJR8L'></th></span></q></dt></tr></i><div id='DJR8L'><tfoot id='DJR8L'></tfoot><dl id='DJR8L'><fieldset id='DJR8L'></fieldset></dl></div>

        cin 在一个 while 循环中

        cin inside a while loop(cin 在一个 while 循环中)

          • <legend id='bUG9b'><style id='bUG9b'><dir id='bUG9b'><q id='bUG9b'></q></dir></style></legend>

            <tfoot id='bUG9b'></tfoot>

              <tbody id='bUG9b'></tbody>

                • <bdo id='bUG9b'></bdo><ul id='bUG9b'></ul>

                  <i id='bUG9b'><tr id='bUG9b'><dt id='bUG9b'><q id='bUG9b'><span id='bUG9b'><b id='bUG9b'><form id='bUG9b'><ins id='bUG9b'></ins><ul id='bUG9b'></ul><sub id='bUG9b'></sub></form><legend id='bUG9b'></legend><bdo id='bUG9b'><pre id='bUG9b'><center id='bUG9b'></center></pre></bdo></b><th id='bUG9b'></th></span></q></dt></tr></i><div id='bUG9b'><tfoot id='bUG9b'></tfoot><dl id='bUG9b'><fieldset id='bUG9b'></fieldset></dl></div>

                  <small id='bUG9b'></small><noframes id='bUG9b'>

                • 本文介绍了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 是如何计算 truefalse 的.

                  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 is x. (You can actually try auto x = cin >> a.
                  • Second step, you are doing (cin >> a) >> b, when we use this intermediate value x, we could write it as x >> 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.

                  因此,当你说truefalse时,你实际上是在谈论这个返回的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) {...} or 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.

                  这篇关于cin 在一个 while 循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                    <bdo id='DeC5u'></bdo><ul id='DeC5u'></ul>

                      1. <i id='DeC5u'><tr id='DeC5u'><dt id='DeC5u'><q id='DeC5u'><span id='DeC5u'><b id='DeC5u'><form id='DeC5u'><ins id='DeC5u'></ins><ul id='DeC5u'></ul><sub id='DeC5u'></sub></form><legend id='DeC5u'></legend><bdo id='DeC5u'><pre id='DeC5u'><center id='DeC5u'></center></pre></bdo></b><th id='DeC5u'></th></span></q></dt></tr></i><div id='DeC5u'><tfoot id='DeC5u'></tfoot><dl id='DeC5u'><fieldset id='DeC5u'></fieldset></dl></div>
                          <tbody id='DeC5u'></tbody>

                        <small id='DeC5u'></small><noframes id='DeC5u'>

                        <legend id='DeC5u'><style id='DeC5u'><dir id='DeC5u'><q id='DeC5u'></q></dir></style></legend>
                        1. <tfoot id='DeC5u'></tfoot>