5

I'm trying to use the split() function provided in boost/algorithm/string.hpp in the following function :

vector<std::string> splitString(string input, string pivot) { //Pivot: e.g., "##"
    vector<string> splitInput;  //Vector where the string is split and stored
    split(splitInput,input,is_any_of(pivot),token_compress_on);       //Split the string
    return splitInput;
}

The following call :

string hello = "Hieafds##addgaeg##adf#h";
vector<string> split = splitString(hello,"##"); //Split the string based on occurrences of "##"

splits the string into "Hieafds" "addgaeg" "adf" & "h". However I don't want the string to be split by a single #. I think that the problem is with is_any_of().

How should the function be modified so that the string is split only by occurrences of "##" ?

4

1 回答 1

7

你是对的,你必须使用is_any_of()

std::string input = "some##text";
std::vector<std::string> output;
split( output, input, is_any_of( "##" ) );

更新

但是,如果你想精确地分割两个锐利,也许你必须使用正则表达式:

 split_regex( output, input, regex( "##" ) ); 

看看文档示例

于 2012-12-14T09:37:14.480 回答