6

一旦我看到一个例子,这可能真的很简单,但是我如何概括 boost::tokenizer 或 boost::split 来处理由多个字符组成的分隔符?

例如,对于“ _ _”,这些标准拆分解决方案似乎都不起作用:

boost::tokenizer<boost::escaped_list_separator<string> > 
        tk(myString, boost::escaped_list_separator<string>("", "____", "\""));
std::vector<string> result;
for (string tmpString : tk) {
    result.push_back(tmpString);
}

或者

boost::split(result, myString, "___");
4

3 回答 3

10
boost::algorithm::split_regex( result, myString, regex( "___" ) ) ;
于 2013-04-03T14:02:52.787 回答
1

您必须改用 splitregex:http ://www.daniweb.com/software-development/cpp/threads/118708/boostalgorithmsplit-with-string-delimeters

于 2013-04-03T14:03:08.910 回答
1

非升压解决方案

vector<string> split(const string &s, const string &delim){
    vector<string> result;
    int start = 0;
    int end = 0;
    while(end!=string::npos){
        end = s.find(delim, start);
        result.push_back(s.substr(start, end-start));
        start = end + delim.length();
    }
    return result;
}
于 2019-07-17T23:12:00.383 回答