5

split()次の関数で提供され ている関数を使用しようとしてboost/algorithm/string.hppいます:

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;
}

次の呼び出し:

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

"Hieafds" "addgaeg" "adf"文字列を&に分割します"h"。ただし、文字列を1つに分割したくありません#。問題はにあると思いますis_any_of()

文字列が?の出現によってのみ分割されるように、関数をどのように変更する必要があり"##"ますか?

4

1 に答える 1

7

そうです、 is_any_of()を使用する必要があります

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

アップデート

ただし、正確に2つのシャープに分割する場合は、正規表現を使用する必要があります。

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

ドキュメントの例を見てください。

于 2012-12-14T09:37:14.480 に答える