【问题标题】:Splitting strings in C++ [duplicate]在 C++ 中拆分字符串 [重复]
【发布时间】:2010-09-21 11:36:08
【问题描述】:

如何在 C++ 中将字符串拆分为标记?

【问题讨论】:

标签: c++ string token


【解决方案1】:

这取决于标记分隔符的复杂程度以及是否有多个。对于简单的问题,只需使用 std::istringstream 和 std::getline。对于更复杂的任务,或者如果您想以符合 STL 的方式迭代令牌,请使用 Boost 的 Tokenizer。另一种可能性(尽管比这两个更混乱)是设置一个 while 循环,该循环调用 std::string::find 并将最后找到的标记的位置更新为搜索下一个标记的起点。但这可能是 3 个选项中最容易出错的选项。

【讨论】:

    【解决方案2】:

    尝试使用字符串流:

    std::string   line("A line of tokens");
    std::stringstream lineStream(line);
    
    std::string token;
    while(lineStream >> token)
    {
    }
    

    查看我对您上一个问题的回答:
    C++ Reading file Tokens

    【讨论】:

      【解决方案3】:

      这对我很有效 :),它将结果放在 elems 中。 delim 可以是任何char

      std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
          std::stringstream ss(s);
          std::string item;
          while(std::getline(ss, item, delim)) {
              elems.push_back(item);
          }
          return elems;
      }
      

      【讨论】:

      • 为什么要退货。什么时候作为引用参数传入函数?
      • 哦,只是为了方便。因此,如果您需要,可以执行以下操作: split(line, ',', elems).at(2);完全没有必要退货。
      • 这不能正确处理空分隔字符串,例如split(",", ',') 应该返回两个空字符串,但上面的代码只返回一个。这可以通过使用“s + delim”初始化 ss 并处理空字符串应返回空列表(而不是具有一个空字符串的列表)的特殊情况来解决。
      【解决方案4】:

      可以使用C函数strtok

      /* strtok example */
      #include <stdio.h>
      #include <string.h>
      
      int main ()
      {
        char str[] ="- This, a sample string.";
        char * pch;
        printf ("Splitting string \"%s\" into tokens:\n",str);
        pch = strtok (str," ,.-");
        while (pch != NULL)
        {
          printf ("%s\n",pch);
          pch = strtok (NULL, " ,.-");
        }
        return 0;
      }
      

      Boost Tokenizer 也可以完成这项工作:

      #include<iostream>
      #include<boost/tokenizer.hpp>
      #include<string>
      
      int main(){
         using namespace std;
         using namespace boost;
         string s = "This is,  a test";
         tokenizer<> tok(s);
         for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
             cout << *beg << "\n";
         }
      }
      

      【讨论】:

      【解决方案5】:

      this Mingw distro 包含 Boost:

      #include <iostream>
      #include <string>
      #include <vector>
      #include <iterator>
      #include <ostream>
      #include <algorithm>
      #include <boost/algorithm/string.hpp>
      using namespace std;
      using namespace boost;
      
      int main() {
          vector<string> v;
          split(v, "1=2&3=4&5=6", is_any_of("=&"));
          copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
      }
      

      【讨论】:

        【解决方案6】:

        另见 boost::split from String Algo library

        string str1("你好 abc-*-ABC-*-aBc 再见"); 矢量 标记; boost::split(tokens, str1, boost::is_any_of("-*")); // 标记 == { "hello abc","ABC","aBc goodbye" }

        【讨论】:

          猜你喜欢
          • 2021-04-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-18
          • 1970-01-01
          相关资源
          最近更新 更多