繁体   English   中英

将字符串拆分为向量 <string> 通过多个字符串定界符

[英]Split a string into a vector<string> by multiple string delimiters

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

  • 用单个char值或多个char值分割字符串

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

     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<string>值:

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

有什么提示吗?

此功能可以执行您想要的操作。 它基本上在定界符的向量上进行迭代,并对每个函数应用您的函数:

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编译器进行编译,并且需要包含<algorithm>标头。

如果您的编译器未编译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;
}

暂无
暂无

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

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