簡體   English   中英

C ++ Boost:拆分函數is_any_of()

[英]C++ Boost: Split function is_any_of()

我正在嘗試在以下函數中使用boost/algorithm/string.hpp中提供的split()函數:

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" 但是我不希望字符串被單個#拆分。 認為問題在於is_any_of()

如何修改函數,以便只通過出現"##"來拆分字符串?

你是對的,你必須使用is_any_of()

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

更新

但是,如果你想分成兩個尖銳的,也許你必須使用正則表達式:

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

看一下文檔示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM