我正在尝试使用以下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;
}
Run Code Online (Sandbox Code Playgroud)
以下电话:
string hello = "Hieafds##addgaeg##adf#h";
vector<string> split = splitString(hello,"##"); //Split the string based on occurrences of "##"
Run Code Online (Sandbox Code Playgroud)
将字符串拆分为"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( "##" ) );
Run Code Online (Sandbox Code Playgroud)
更新
但是,如果你想分成两个尖锐的,也许你必须使用正则表达式:
split_regex( output, input, regex( "##" ) );
Run Code Online (Sandbox Code Playgroud)
看一下文档示例.
| 归档时间: |
|
| 查看次数: |
9065 次 |
| 最近记录: |