/*
tokenizer库是一个专门用于分词(token)的字符串处理库,可以方便的把一个字符串分解成若干个单词,
它比string_algo的split更加强大 需要加头文件 #include<boost/tokenizer.hpp>
tokenizer类接受三个模板类型参数:
template<class TokenizerFunc = char_delimiters_separator<char>,
class Iterator = std::string::const_iterator,
class Type = std::string
>
这三个模板类型都提供了默认值:
1、TokenizerFunc 分词函数对象,默认空格或标点符号
2、字符序列迭代器类型
3、分词结果的类型
tokenizer库提供预定义好的四个分词对象,它们是:
1、char_delimiters_separator: tokenizer默认使用的分词函数对象
2、char_separator:它支持一个字符集合作为分隔符号,默认的行为与char_delimiter_separator类似
3、escaped_list_separator:用csv格式(逗号分隔)的分词;
4、offset_separator:使用偏移量来分词,在分解平文件格式的字符串时有用
*** char_separator:
使用一个字符集合作为分词依据,行为很类似split算法,他的构造函数声明是:
char_separator(const char* dropped_delims,
const char* kept_delims = 0,
empty_token_policy empty_tokens = drop_empty_tokens);
参数含义如下:
1、dropped_delims分割符集合,不会作为分词的结果出现;===指定分隔符号
2、kept_delims分隔符集合,但其中的字符会保留在分词结果中;
3、empty_tokens类似于split算法eCompress参数,处理连续出现的分隔符
如为keep_empty_tokens则表示连续出现的分隔符标识了一个空白字符串,相当split的token_compress_off值
如为drop_empty_tokens则空白单词不会作为分词的结果
****escaped_list_separator 逗号分割 一般都用默认值
escaped_list_separator是专门处理CSV格式(Comma Split Value,逗号分隔值)的分词对象
构造函数声明是:
escaped_list_separator(char e ='\\' ,char c = ',', char q = '\"')
escaped_list_separator的构造函数参数一般使用默认值,含义如下:
1\] e指定了字符串中转义字符,默认是斜杠\\; \[2\] c是分隔符,默认是逗号; \[3\] q是引号字符,默认是"; \*\*\*\*offest_separator \* 分词函数对象不基于查找分隔符,而是使用偏移量的概念,在处理某些不使用分隔符二使用固定字段宽度的文本时很有用 \* 声明 \* offset_separator(lter begin, lter end, bool wrap_offsets = true, bool return_partial_last = true); \[1\] \[2\] begin 和 end 指定分词用的序列,序列的每个元素指定分词宽度 \[3\] wrap_offsets 决定是都在偏移量用完后继续分词 \[4\] return_partial_last 决定是否返回分词不足的部分 \*/ #include\
#include\ #include\ using namespace std; using namespace boost; int main() { // 分词演示 默认 string str ="My!name,is.Mr?HOU!"; tokenizer\<\> tok(str); //缺省模板参数创建分词对象 默认采用空格或者标点分词 for (BOOST_AUTO(it, tok.begin()); it != tok.end(); it++) { cout \<\< \*it \<\< endl; } // char_separator分词 cout \<\< endl \<\< "char_separator tokenizer" \<\< endl; string str_c = "My\ is?Mr\<,Huang"; // 第一个引号内的参数是作为分割后不保留的内容,第二个引号内的参数是作为分割后保留的内容 // \<或\>进行分割 不保留\<\> , ? 保留 // 第三个参数:是否保留空的分词结果 keep_empty_tokens保留空 drop_empty_tokens 不保留空 char_separator\ sep("\<\>", ",?", keep_empty_tokens); tokenizer\ \> token_c(str_c, sep); // 参数1是字符串内容 参数2是分词对象 for (BOOST_AUTO(it, token_c.begin()); it != token_c.end(); it++) { cout \<\< \*it \<\< endl; } cout \<\< endl; // escaped_list_separator分词 默认以逗号分割 \\\\转义符号 cout \<\< "escaped_list_separator" \<\< endl; string str3("My,name,is,Mr,\\\\\\"Huang!\\\\\\""); escaped_list_separator\ sep_es; tokenizer\ \> tok3(str3, sep_es); for (BOOST_AUTO(it, tok3.begin()); it != tok3.end(); ++it) cout \<\< \*it \<\< endl; cout \<\< endl; // offset_separator cout \<\< "offset_separator" \<\< endl; //int arr\[3\] = { 2,3,4 }; //每次分割的字符长度 循环往复的 int arr\[3\] = { 2,3,15 }; string str4 = "223334444"; // 参数3 /\*决定偏移用完后是否继续分词\*/ 参数4 决定是否返回分词不足的部分 offset_separator sep4(arr, arr + 3, false, false); tokenizer\ tok4(str4, sep4); for (BOOST_AUTO(it, tok4.begin()); it != tok4.end(); ++it) cout \<\< \*it \<\< endl; cout \<\< endl; return 0; }
C++ Boost库【4】 --分词器的使用
小侯不躺平.2026-05-14 10:43