简体   繁体   English

用 C++ 中的分隔符拆分字符串

[英]Splitting a string with delimiter in C++

There are a few examples about this question.关于这个问题有几个例子。 However most of the answers are not what I am looking for.然而,大多数答案都不是我想要的。

I am looking for a way to implement an efficient and easy function rather than using boost or any other non STL libraries.我正在寻找一种方法来实现高效且简单的功能,而不是使用 boost 或任何其他非 STL 库。 If you ask me why, in most coding competitions and interviews, you are not allowed to use them.如果你问我为什么,在大多数编码竞赛和面试中,你都不允许使用它们。

Here is the closest that I can approach:这是我可以接近的最接近的:

vector<string> SplitString(const char *str, char c)
{
    vector<string> result;
    do {
        const char *begin = str;
        while(*str != c && *str) {
            str++;
        }
        result.push_back(string(begin, str));
    } while (0 != *str++);
    return result;
}

int main() {

    string mainString = "This is a sentence. Another sentence. The third sentence. This is the last sentence.";
    vector<string> sentences;
    sentences = SplitString(mainString.c_str(), '.');
    while (!sentences.empty()) {
        cout << sentences.back() << endl;
        sentences.pop_back();
    }
    return 0;
}

Now the problem with this is, it can only have a char delimiter not string.现在的问题是,它只能有一个字符分隔符而不是字符串。 I have thought of implementing a few ways but they seemed way too complex.我想过实现一些方法,但它们似乎太复杂了。 The easiest one that I thought was, convert delimiter to char array use c as the first char of the delimiter char array after this:我认为最简单的方法是,将分隔符转换为字符数组,在此之后使用c作为分隔符字符数组的第一个字符:

while(*str != c && *str) {
    str++;
}
const char *beginDelim = *cArr;
while(1) {
    if (*str == *cArr && *str && *cArr) {
       str++;
       cArr++;
    }
    else if (!*cArr) {
        break;
    }
    else if (*cArr) {
        cArr = beginDelim;
    }
}

And the code continues from result.push_back() part.代码从result.push_back()部分继续。

So I was wondering if are there any way to implement an efficient and easy function for splitting a string with a string delimiter?所以我想知道是否有任何方法可以实现一个高效且简单的函数来用字符串分隔符分割字符串?

Generally speaking, a string is a char pointer.一般来说,字符串是一个字符指针。 So you should search for the first character in the delimeter, then check the very next character.所以你应该搜索分隔符中的第一个字符,然后检查下一个字符。 Also in looking at your code I am not sure that while (0 != *str++) is doing what you think it is.同样在查看您的代码时,我不确定 while (0 != *str++) 正在做您认为的事情。 I think you mean for it to be null terminated.我认为你的意思是它是空终止的。

something like this should do it:这样的事情应该这样做:

vector<string> SplitString(const char* str,const char* d) {
  vector<string> result;
  size_t len = strlen(d);
  const char* start = str;
  while ( str = strstr(start,d) ) {
    result.push_back(string(start,len));
    start = str + len;
  }
  result.push_back(start);
  return result;
}

How's this:这个怎么样:

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

using namespace std;

vector<string> SplitString(const string &str, const string &delim)
{
    vector<string> ret;
    string::const_iterator prev = str.begin();

    for (string::const_iterator i = str.begin(); i < str.end() - delim.length()+1; ++i)
    {
        if (equal(delim.begin(), delim.end(), i)) {
            ret.push_back(string(prev,i));
            i += delim.length()-1;
            prev = i+1;
        }
    }

    ret.push_back(string(prev,str.end()));

    return ret;
}
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> SplitString(string str, const string &delim) {
    vector<string> result;
    size_t found;
    while((found = str.find(delim)) != string::npos) {
        result.push_back(str.substr(0, found));
        str = str.substr(found + delim.size());
    }
    return result;
}

int main() {
    string mainString = "This is a sentence. Another sentence. The third sentence. This is the last sentence.";
    vector<string> sentences;

    sentences = SplitString(mainString, ".");

    for(auto& sentence : sentences) {
        cout << sentence << endl;   
    }
    return 0;
}
vector<string>split(string str, const char d){
    string temp;
    vector<string>vct;
    for(int i = 0; str[i] != '\0'; i++){
        if(str[i] != d){
            temp += str[i];
        }else if(!empty(temp)){
                vct.push_back(temp), temp.clear();
        }
    }
    vct.push_back(temp);
    return vct;
}

Takes two arguments需要两个参数

  • const char d as delimiter. const char d作为分隔符。
  • string str as string to be splitted. string str作为要拆分的字符串。

stores splitted string in a vector and returns it.将拆分后的字符串存储在一个vectorreturns它。 Although, I'm not sure about efficiency of this code.虽然,我不确定这段代码的效率。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM