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

时间:2012-12-14 09:31:37

标签: c++ boost

我尝试在以下函数中使用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"&amp; "h"。但是,我不希望字符串被单个#拆分。我认为问题出在is_any_of()

如何修改函数,以便仅按出现"##"来分割字符串?

1 个答案:

答案 0 :(得分: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( "##" ) ); 

看看documentation example