<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 命令会在缓冲区中留下一个 ' '?

        Why does cin command leaves a #39;#39; in the buffer?(为什么 cin 命令会在缓冲区中留下一个 ?)
      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 命令会在缓冲区中留下一个 ' '?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                • <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'>

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

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