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

      <small id='47rbl'></small><noframes id='47rbl'>

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

    2. C++ 拆分字符串

      C++ split string(C++ 拆分字符串)
    3. <legend id='wSC8S'><style id='wSC8S'><dir id='wSC8S'><q id='wSC8S'></q></dir></style></legend>
    4. <i id='wSC8S'><tr id='wSC8S'><dt id='wSC8S'><q id='wSC8S'><span id='wSC8S'><b id='wSC8S'><form id='wSC8S'><ins id='wSC8S'></ins><ul id='wSC8S'></ul><sub id='wSC8S'></sub></form><legend id='wSC8S'></legend><bdo id='wSC8S'><pre id='wSC8S'><center id='wSC8S'></center></pre></bdo></b><th id='wSC8S'></th></span></q></dt></tr></i><div id='wSC8S'><tfoot id='wSC8S'></tfoot><dl id='wSC8S'><fieldset id='wSC8S'></fieldset></dl></div>

      <tfoot id='wSC8S'></tfoot>

          <tbody id='wSC8S'></tbody>
      • <small id='wSC8S'></small><noframes id='wSC8S'>

            <bdo id='wSC8S'></bdo><ul id='wSC8S'></ul>
              • 本文介绍了C++ 拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用空格作为分隔符来分割字符串.我想将每个标记存储在一个数组或向量中.

                I am trying to split a string using spaces as a delimiter. I would like to store each token in an array or vector.

                我试过了.

                    string tempInput;
                    cin >> tempInput;
                    string input[5];
                
                    stringstream ss(tempInput); // Insert the string into a stream
                    int i=0;
                    while (ss >> tempInput){
                        input[i] = tempInput;
                        i++;
                    }
                

                问题是,如果我输入这是一个测试",数组似乎只存储输入[0] =这个".它不包含 input[2] 到 input[4] 的值.

                The problem is that if i input "this is a test", the array only seems to store input[0] = "this". It does not contain values for input[2] through input[4].

                我也尝试过使用向量,但结果相同.

                I have also tried using a vector but with the same result.

                推荐答案

                转到重复题学习如何将字符串拆分为单词,但您的方法实际上是正确的.实际问题在于您如何在尝试拆分输入之前 读取它:

                Go to the duplicate questions to learn how to split a string into words, but your method is actually correct. The actual problem lies in how you are reading the input before trying to split it:

                string tempInput;
                cin >> tempInput; // !!!
                

                当您使用 cin >>tempInput,您只能从输入中获取第一个单词,而不是整个文本.有两种可能的方法来解决这个问题,其中最简单的方法是忘记 stringstream 并直接迭代输入:

                When you use the cin >> tempInput, you are only getting the first word from the input, not the whole text. There are two possible ways of working your way out of that, the simplest of which is forgetting about the stringstream and directly iterating on input:

                std::string tempInput;
                std::vector< std::string > tokens;
                while ( std::cin >> tempInput ) {
                   tokens.push_back( tempInput );
                }
                // alternatively, including algorithm and iterator headers:
                std::vector< std::string > tokens;
                std::copy( std::istream_iterator<std::string>( std::cin ),
                           std::istream_iterator<std::string>(),
                           std::back_inserter(tokens) );
                

                这种方法将在单个向量中为您提供输入中的所有标记.如果您需要单独处理每一行,那么您应该使用 <string> 标头中的 getline 而不是 cin >>临时输入:

                This approach will give you all the tokens in the input in a single vector. If you need to work with each line separatedly then you should use getline from the <string> header instead of the cin >> tempInput:

                std::string tempInput;
                while ( getline( std::cin, tempInput ) ) { // read line
                   // tokenize the line, possibly with your own code or 
                   // any answer in the 'duplicate' question
                }
                

                这篇关于C++ 拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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?)

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

                  • <bdo id='nxWGU'></bdo><ul id='nxWGU'></ul>
                    <tfoot id='nxWGU'></tfoot>
                        <tbody id='nxWGU'></tbody>

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