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

    <tfoot id='TBTDy'></tfoot>

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

      <legend id='TBTDy'><style id='TBTDy'><dir id='TBTDy'><q id='TBTDy'></q></dir></style></legend>

        读取由空格或换行符分隔的输入...?

        read input separated by whitespace(s) or newline...?(读取由空格或换行符分隔的输入...?)

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

              <tbody id='j0WCd'></tbody>
              <bdo id='j0WCd'></bdo><ul id='j0WCd'></ul>

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

            • <tfoot id='j0WCd'></tfoot>
                1. 本文介绍了读取由空格或换行符分隔的输入...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从标准输入流中获取输入.比如,

                  I'm grabbing input from a standard input stream. Such as,

                  1 2 3 4 5
                  

                  1
                  2
                  3
                  4
                  5
                  

                  我正在使用:

                  std::string in;
                  std::getline(std::cin, in);
                  

                  但这只是抓住了换行符,对吗?如何仅使用 iosteam、字符串和 cstdlib 获取输入是否由换行符或空格分隔?

                  But that just grabs upto the newline, correct? How can I get input whether they are separated by newline OR whitespace(s) using only iosteam, string, and cstdlib?

                  推荐答案

                  只需使用:

                  your_type x;
                  while (std::cin >> x)
                  {
                      // use x
                  }
                  

                  operator>> 默认会跳过空格.您可以链接事物以一次读取多个变量:

                  operator>> will skip whitespace by default. You can chain things to read several variables at once:

                  if (std::cin >> my_string >> my_number)
                      // use them both
                  

                  getline() 读取一行中的所有内容,无论它是空的还是包含数十个空格分隔的元素,都返回.如果您提供可选的替代分隔符 ala getline(std::cin, my_string, ' ') 它仍然不会执行您似乎想要的操作,例如标签将被读入 my_string.

                  getline() reads everything on a single line, returning that whether it's empty or contains dozens of space-separated elements. If you provide the optional alternative delimiter ala getline(std::cin, my_string, ' ') it still won't do what you seem to want, e.g. tabs will be read into my_string.

                  这可能不需要,但您可能很快会感兴趣的一个相当常见的要求是读取单个换行符分隔的行,然后将其拆分为组件...

                  Probably not needed for this, but a fairly common requirement that you may be interested in sometime soon is to read a single newline-delimited line, then split it into components...

                  std::string line;
                  while (std::getline(std::cin, line))
                  {
                      std::istringstream iss(line);
                      first_type first_on_line;
                      second_type second_on_line;
                      third_type third_on_line;
                      if (iss >> first_on_line >> second_on_line >> third_on_line)
                          ...
                  }
                  

                  这篇关于读取由空格或换行符分隔的输入...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <tbody id='moUGn'></tbody>

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

                      • <bdo id='moUGn'></bdo><ul id='moUGn'></ul>
                      • <tfoot id='moUGn'></tfoot>
                            <legend id='moUGn'><style id='moUGn'><dir id='moUGn'><q id='moUGn'></q></dir></style></legend>