简体   繁体   中英

C++ Boost: Split function is_any_of()

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 "##" ?

You're right, you have to use is_any_of()

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

update

But, if you want to split on exactly two sharp, maybe you have to use a regular expression:

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

take a look at the documentation example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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