【问题标题】:Split a string into a vector<string> by multiple string delimiters通过多个字符串分隔符将字符串拆分为 vector<string>
【发布时间】:2017-07-26 16:04:28
【问题描述】:

在 C++ 中,不使用任何 boost 例程,我们知道如何:

  • split a string by a single char value or multiple char values

  • 用单个字符串值分割字符串:

    void ParseStringByStringSeparator(string s, const string separator, vector<string>& result)
    {
        result.clear();
        size_t pos = 0;
        string token;
    
        while ((pos = s.find(separator)) != string::npos) {
            token = s.substr(0, pos);
            result.push_back(token);
            s.erase(0, pos + separator.length());
        }
        result.push_back(s);
    }
    

但是如何用多个字符串值分割一个字符串呢?

例如,如果我有以下字符串值"Hello I am a String",而我的分隔符是" ""am",那么我想获得以下vector&lt;string&gt; 值:

{"Hello","I","a","String"}

有什么提示吗?

【问题讨论】:

  • 使用第一个分隔符分割字符串,然后使用第二个分隔符分割结果。
  • 查找所有其他分隔符并将其替换为第一个分隔符并使用第一个分隔符进行拆分?
  • 这可行,但如果第一个分隔符不是单个字符,它可能会失败 - 因为替换单词时可能会无意中在字符串中创建另一个分隔符。
  • 越想越觉得ikleschenkov是对的。使用第一个分隔符拆分字符串,然后将每个子字符串除以下一个,并为每个下一个重复此操作。
  • 如果一个分隔符包含其他分隔符怎么办?最大的胜利?

标签: c++ string split


【解决方案1】:

这个函数做你想做的事。它基本上迭代分隔符的向量,为每个分隔符应用你的函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {

    //The result to be returned
    vector<string> result = { s };

    //Iterate on the list of separators, so for each separators
    for (const auto& sep : separators) {
        //toReplaceBy will be the next vector of strings where it will iterate
        vector<string> toReplaceBy, tempRes;

        //For each strings, we will split on "sep", the separator
        for (auto&a : result) {
            //Because of the result vector being cleared every time your function get called
            //It get in a temp vector that we will concatenate after
            ParseStringByStringSeparator(a, sep, tempRes);
            //Concatenation of theses vectors
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }

        //Erasing all strings that are empty. C++11 code here required because of the lambda
        toReplaceBy.erase(std::remove_if(toReplaceBy.begin(), toReplaceBy.end(),
            [](const std::string& i) {
            return i == "";
        }), toReplaceBy.end());

        //The vector containing strings to be splited is replaced by the split result on this iteration
        result = toReplaceBy;
        //And we will split those results using the next separator, if there's more separator to iterate on
    }
    return result;
}

要测试的代码:

string test = "Hello I am a string";

auto r = SplitStringMultipleStrParameters(test, { " ", "am" });

for (auto& a : r) {
    std::cout << a << '\n';
}

需要用C++11编译器编译,需要包含&lt;algorithm&gt;头文件。

如果你的编译器不能编译 C++11 代码,这里是函数:

vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {
    vector<string> result = { s };
    for (const auto& sep : separators) {
        vector<string> toReplaceBy, tempRes;
        for (auto&a : result) {
            ParseStringByStringSeparator(a, sep, tempRes);
            toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
        }
        auto it = toReplaceBy.begin();
        while (it != toReplaceBy.end()) {
            if ((*it) == "")
                it = toReplaceBy.erase(it);
            else
                ++it;
        }
        result = toReplaceBy;
    }
    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 2011-11-29
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2017-03-21
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多