C++ Boost库【4】 --分词器的使用

/*

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<iostream>

#include<boost/tokenizer.hpp>

#include<boost/typeof/typeof.hpp>

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<name>is?Mr<,Huang";

// 第一个引号内的参数是作为分割后不保留的内容,第二个引号内的参数是作为分割后保留的内容

// <或>进行分割 不保留<> , ? 保留

// 第三个参数:是否保留空的分词结果 keep_empty_tokens保留空 drop_empty_tokens 不保留空

char_separator<char> sep("<>", ",?", keep_empty_tokens);

tokenizer<char_separator<char>> 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<char> sep_es;

tokenizer<escaped_list_separator<char>> 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 arr3 = { 2,3,4 }; //每次分割的字符长度 循环往复的

int arr3 = { 2,3,15 };

string str4 = "223334444";

// 参数3 /*决定偏移用完后是否继续分词*/ 参数4 决定是否返回分词不足的部分

offset_separator sep4(arr, arr + 3, false, false);

tokenizer<offset_separator> tok4(str4, sep4);

for (BOOST_AUTO(it, tok4.begin()); it != tok4.end(); ++it)

cout << *it << endl;

cout << endl;

return 0;

}

相关推荐
Lumbrologist2 小时前
【C++】零基础入门 · 第 17 节:多线程编程基础
java·c++·算法
sun0077002 小时前
Windows下UniGetUI,Linux下敲命令
windows
流星白龙3 小时前
【MySQL高阶】18.缓冲池页管理
数据库·windows·mysql
A_humble_scholar3 小时前
C++11 学习笔记:统一初始化、右值引用与完美转发
c++·笔记·学习
叶子野格3 小时前
《C语言学习:位运算》17
c语言·开发语言·c++·学习·visual studio
晚风吹红霞4 小时前
C++ stack 和 queue 完全指南:适配器模式与双端队列的奥秘
c++·算法·适配器模式
AI行业学习4 小时前
PuTTY 工具下载部署、基础配置及 SSH 远程服务器连接完整操作指南Windows 平台 【2026.6.1】
运维·windows·ssh
代码改善世界4 小时前
【C++进阶】红黑树模拟实现mymap和myset
开发语言·c++
tealcwu4 小时前
【Unity实战】Unity IAP 4.x 在 Windows Store (UWP) 平台上的实现指南
windows·unity·游戏引擎
断点之下4 小时前
从C的struct到C++的class:封装、this指针、三大特性入门
开发语言·c++