【问题标题】:finding substring c++ [duplicate]查找子字符串c ++ [重复]
【发布时间】:2010-08-10 09:55:28
【问题描述】:

可能重复:
How to split a string?

嗨,

我有一个字符串说“1,0,1”,我怎样才能得到用逗号分隔的子字符串。

【问题讨论】:

标签: c++


【解决方案1】:

C++ 并没有内置函数来执行此操作。但是,它可以使用std::string::find_first_of 成员函数或非成员std::find 来实现。

这是使用后者的示例:

#include <string>
#include <vector>
#include <algorithm>


// given a string str, split it on every occurrence of the character delim
std::vector<std::string> tokenize(std::string str, char delim) {
    // store the results in a vector of strings
    std::vector<std::string> tokens;

    std::string::iterator end = str.end();
    std::string::iterator left = str.begin();
    for (;;) {
        // find the next occurrence of the delimiter
        std::string::iterator right = std::find(left, end, delim);
        // create a string from the end of last one up until the one we just foun
        tokens.push_back(std::string(left, right));
        // if we reached the end of the string, exit the loop
        if (right == end) { break; }
        // otherwise, start the next iteration just past the delimiter we just found
        left = right + 1;
    }
    return tokens;
}

// test program
int main() {
    std::string str = "foo, bar, baz";
    std::string str2 = "foo, bar, baz,";
    std::string str3 = "foo";
    std::string str4 = "";
    std::string str5 = ",";

    std::vector<std::string> tokens = tokenize(str, ',');
    std::vector<std::string> tokens2 = tokenize(str2, ',');
    std::vector<std::string> tokens3 = tokenize(str3, ',');
    std::vector<std::string> tokens4 = tokenize(str4, ',');
    std::vector<std::string> tokens5 = tokenize(str5, ',');
}

当然,有很多边界情况需要处理,而且这个实现可能不会完全符合您的要求,但它应该为您提供一个起点。

【讨论】:

  • 返回向量不是个好主意,最好是通过引用传递的函数参数。
  • find 也可作为std::string 的成员函数使用。
  • @Guillaume:为什么?所有主要编译器都实现了 NRVO,即使是在低级别优化。
【解决方案2】:

另一种方法是使用strtok。这是一种旧的 c 方式,但它仍然适用于该问题。

using <vector>
using <string>

char* token, line[512];
std::string tokenStr;
std::string lineStr = "0, 1, 2";
std::vector<std::string> commaSplit;
strcpy ( line, lineStr.c_str());

//Remove spaces and find the first instance of ','
token = strtok( line, " ," );
while(token != NULL)
{
    //Copy the token to a string
    tokenStr = token;

    //Add the token to the vector
    commaSplit.push_back(token);

    //Find next instance of the ,
    token = strtok(NULL, " ,");
}

【讨论】:

    【解决方案3】:

    在谷歌上搜索一种算法来explodetokenize你的字符串。这是微不足道的。

    您还可以查看文档并使用可用工具:http://www.cplusplus.com/reference/string/string/

    一个简单的实现可能是:

    void tokenize(const string & text, vector<string> & tokens, char delim)
    {
      size_t length = text.size();
      string token = "";
    
      for(size_t i=0;i<length;i++)
      {
          if(text[i] != delim)
          {
            token += text[i];
          }
          else
          {
            if(token.size() > 0)
            {
              tokens.push_back(token);
            }
            token = "";
          }
      }
      tokens.push_back(token);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2020-02-24
      • 2017-01-14
      • 2016-10-05
      • 1970-01-01
      • 2013-09-27
      • 2019-03-01
      相关资源
      最近更新 更多