1. <small id='GTWKH'></small><noframes id='GTWKH'>

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

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

    1. <tfoot id='GTWKH'></tfoot>

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

      时间:2023-10-06

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

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

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

          • <bdo id='ChXo6'></bdo><ul id='ChXo6'></ul>
                <tbody id='ChXo6'></tbody>
              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)
                        ...
                }
                

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

                上一篇:重置cin流状态C++ 下一篇:序列化包含 std::string 的类

                相关文章

                最新文章

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

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