<tfoot id='NrNu8'></tfoot>
    • <bdo id='NrNu8'></bdo><ul id='NrNu8'></ul>

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

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

        为什么 cin 命令会在缓冲区中留下一个 ' '?

        时间:2023-09-27
      2. <i id='DqSA4'><tr id='DqSA4'><dt id='DqSA4'><q id='DqSA4'><span id='DqSA4'><b id='DqSA4'><form id='DqSA4'><ins id='DqSA4'></ins><ul id='DqSA4'></ul><sub id='DqSA4'></sub></form><legend id='DqSA4'></legend><bdo id='DqSA4'><pre id='DqSA4'><center id='DqSA4'></center></pre></bdo></b><th id='DqSA4'></th></span></q></dt></tr></i><div id='DqSA4'><tfoot id='DqSA4'></tfoot><dl id='DqSA4'><fieldset id='DqSA4'></fieldset></dl></div>
        <legend id='DqSA4'><style id='DqSA4'><dir id='DqSA4'><q id='DqSA4'></q></dir></style></legend>
              <bdo id='DqSA4'></bdo><ul id='DqSA4'></ul>
            • <tfoot id='DqSA4'></tfoot>
                <tbody id='DqSA4'></tbody>

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

                • 本文介绍了为什么 cin 命令会在缓冲区中留下一个 ' '?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这与:cin 和 getline 跳过输入有关回答它发生的原因以及如何修复它.

                  This is related to: cin and getline skipping input But they don't answer the why it happens just the how to fix it.

                  为什么 cin 会在缓冲区中留下一个 ' ' 而只有 cin.getline 接受它?

                  Why does cin leaves a ' ' in the buffer but just cin.getline takes it?

                  例如:

                  cin >> foo;
                  cin >> bar;//No problem
                  cin >> baz;//No problem.
                  

                  但是使用 cin.getline

                  cin >> foo;
                  cin.getline(bar,100);//will take the '
                  '
                  

                  那么为什么 cin 不会发生这种情况,而 cin.getline 会发生这种情况?

                  So why it doesn't happen with cin but it does with cin.getline?

                  推荐答案

                  因为,当你说 getline 时,你说你想要得到一行...一行是以 ,结尾是其中不可或缺的一部分.

                  Because, when you say getline you say you want to get a line... A line is string that ends with , the ending is integral part of it.

                  当你说 cin >>东西,你想要精确地获得something,仅此而已.结束线标记不是它的一部分,所以它不会被消耗.除非你有一个特殊的 line 类型,但标准库中没有这样的东西.

                  When you say cin >> something, you want to get precisely something, nothing more. End-line marker is not part of it, so it's not consumed. Unless you would have a special type for line, but there is no such thing in standard library.

                  虽然没有引用标准,这可能被视为意见,但这是其背后的逻辑.它们有不同的语义.还有另一个区别 getline 作为未格式化的输入,以及 operator>> 作为 格式化输入.我强烈建议阅读这些链接以了解差异.这也说明它们在语义上是不同的.

                  While without citations from standard this might be taken as opinion, but this is logic behind it. They have different semantics. There is also another difference getline works as unformatted input, and operator>> works as formatted input. I strongly suggest reading of those links to get the differences. This also says that they are semantically different.

                  另一个答案,无论好坏是有争议的,将引用标准,我肯定会说,getline 的行为方式,以及 operator>> 的行为方式不同的类型,比如说,它是这样工作的,因为标准是这样说的.这会很好,因为该标准绝对定义了事物的工作方式,并且可以任意执行......而且它很少解释设计背后的动机和逻辑.

                  Another answer, better or not is debatable, would be to quote standard, that I am sure says, how getline behaves, and how operator>> behaves for different types, and say, it works like this, because standard says so. This would be good, because the standard absolutely defines how things work and it can do so arbitrarily... And it rarely does explain motivation and logic behind the design.

                  这篇关于为什么 cin 命令会在缓冲区中留下一个 ' '?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有办法在 C++ 源代码中用空格编写大量数字以使其更具可读性? 下一篇:C++ 数组初始化

                  相关文章

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

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

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

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

                      <tfoot id='ytrZy'></tfoot>