简体   繁体   English

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

[英]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 : 我正在尝试在以下函数中使用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;
}

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" . 将字符串分成"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() . 认为问题在于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() 你是对的,你必须使用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 . 看一下文档示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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