boost-tokenizer学习

tokenizer库是一个专门用于分词(token)的字符串处理库;
可以使用简单易用的方法把一个字符串分解成若干个单词;
tokenizerl类是该库的核心,它以容器的外观提供分词序列;
TokenizerFunc:专门的分词函数对象,默认使用空格和标点分词

  • char_delimiters_separator         使用标点符号分词
  • char_separator                          使用字符集合作为分词符
  • escaped_list_separator             使用CSV的逗号分割
  • offset_separator                         使用偏移量来分词

缺陷:
1、只支持使用单个字符进行分词;
2、对wstring(UNICODE)缺乏完善的考虑;

正则表达式xpressive和string_algo可以提供更好的实现,可以对字符串操作工作的更好!

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 
/*
    tokenizer库是一个专门用于分词(token)的字符串处理库;
    可以使用简单易用的方法把一个字符串分解成若干个单词;
    tokenizerl类是该库的核心,它以容器的外观提供分词序列;
    TokenizerFunc:专门的分词函数对象,默认使用空格和标点分词
    char_delimiters_separator    使用标点符号分词
    char_separator               使用字符集合作为分词符
    escaped_list_separator       使用CSV的逗号分割
    offset_separator             使用偏移量来分词

缺陷:
    1、只支持使用单个字符进行分词;
    2、对wstring(UNICODE)缺乏完善的考虑;

正则表达式xpressive和string_algo可以提供更好的实现,可以对字符串操作工作的更好!
*/

/*
template <
typename TokenizerFunc = char_delimiters_separator<char>, 
typename Iterator = std::string::const_iterator,
typename Type = std::string
>
class tokenizer {
private:
typedef token_iterator_generator<TokenizerFunc,Iterator,Type> TGen;

// It seems that MSVC does not like the unqualified use of iterator,
// Thus we use iter internally when it is used unqualified and
// the users of this class will always qualify iterator.     
typedef typename TGen::type iter;

public:

typedef iter iterator;
typedef iter const_iterator;
typedef Type value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const pointer const_pointer;
typedef void size_type;
typedef void difference_type;

tokenizer(Iterator first, Iterator last,
const TokenizerFunc& f = TokenizerFunc()) 
: first_(first), last_(last), f_(f) { }

template <typename Container>
tokenizer(const Container& c)
: first_(c.begin()), last_(c.end()), f_() { }

template <typename Container>
tokenizer(const Container& c,const TokenizerFunc& f)
: first_(c.begin()), last_(c.end()), f_(f) { }

void assign(Iterator first, Iterator last){
first_ = first;
last_ = last;
}

void assign(Iterator first, Iterator last, const TokenizerFunc& f){
assign(first,last);
f_ = f;
}

template <typename Container>
void assign(const Container& c){
assign(c.begin(),c.end());
}

template <typename Container>
void assign(const Container& c, const TokenizerFunc& f){
assign(c.begin(),c.end(),f);
}

iter begin() const { return iter(f_,first_,last_); }
iter end() const { return iter(f_,last_,last_); }

*/

/************************************************************************/
/* C++ stl Library                                                        */
/************************************************************************/
#include <iostream>
#include <string>

/************************************************************************/
/* C++ boost Library                                                   */
/************************************************************************/
#include "boost/tokenizer.hpp"
#include <boost/typeof/typeof.hpp>

using namespace boost;
using namespace std;

template<typename T>
void print(T &tok)
{
    for(BOOST_AUTO(pos, tok.begin()); pos != tok.end(); pos++)
    {
        cout << "[" << *pos << "]" ;
    }
    cout << endl;
}

int main(void)
{
    //char_delimiters_separator
    string str1 = "I love my town!xian";
    tokenizer<> tok1(str1);          //默认使用空格和标点分词
    print(tok1);

string str2 = "I,love,my,town!";
    tokenizer<> tok2(str2);          //默认使用空格和标点分词
    print(tok2);

//char_separator 
    string str3("I love my town!xian");  
    char_separator<char> sep;  
    tokenizer<char_separator<char> > tok3(str3, sep);  
    print(tok3);

string str4 = ";!!;Hello|world||-Michael--Joessy;yoo;handsome|";  
    char_separator<char> sep1("-;|");  
    tokenizer<char_separator<char> > tok4(str4, sep1);  
    print(tok4);

char_separator<char> sep2("-;", "|", keep_empty_tokens);  
    tokenizer<char_separator<char> > tok5(str4, sep2);  
    print(tok5);

//escaped_list_separator 
    string str5 = "aa,Int32,localTag1,23";  
    tokenizer<escaped_list_separator<char> > tok6(str5); 
    print(tok6);

//offset_separator             
    string str6 = "1225200140023";

};  
    offset_separator f(offsets, offsets + );  
    tokenizer<offset_separator> tok7(str6, f);  
    print(tok7);

cin.get();
    ;
}

boost-tokenizer分词库学习的更多相关文章

  1. 【Todo】Boost安装与学习

    现在这里找下载包 http://sourceforge.net/projects/boost 我找的是 1_62_0 下面是从公司wiki上找到的一个说明. boost & thrift安装步 ...

  2. 【Boost】boost::tokenizer详解

    分类: [C++]--[Boost]2012-12-28 21:42 2343人阅读 评论(0) 收藏 举报   目录(?)[+]   tokenizer 库提供预定义好的四个分词对象, 其中char ...

  3. boost::tokenizer详解

    tokenizer 库提供预定义好的四个分词对象, 其中char_delimiters_separator已弃用. 其他如下: 1. char_separator char_separator有两个构 ...

  4. boost::tuple 深入学习解说

    #include<iostream> #include<string> #include<boost/tuple/tuple.hpp> #include<bo ...

  5. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  6. boost asio io_service学习笔记

    构造函数 构造函数的主要动作就是调用CreateIoCompletionPort创建了一个初始iocp. Dispatch和post的区别 Post一定是PostQueuedCompletionSta ...

  7. boost timer代码学习笔记

    socket连接中需要判断超时 所以这几天看了看boost中计时器的文档和示例 一共有五个例子 从简单的同步等待到异步调用超时处理 先看第一个例子 // timer1.cpp: 定义控制台应用程序的入 ...

  8. Boost.Coroutine2:学习使用Coroutine(协程)

    function(函数)routine(例程)coroutine (协程) 函数,例程以及协程都是指一系列的操作的集合. 函数(有返回值)以及例程(没有返回值)也被称作subroutine(子例程), ...

  9. Lucene 中的Tokenizer, TokenFilter学习

      lucene中的TokenStream,TokenFilter之间关系   TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...

随机推荐

  1. 算法笔记_092:蓝桥杯练习 c++_ch04_02_修正版(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 [题目描述] 实现一个时间类Time.将小时,分钟和秒存储为int型成员变量.要求该类中包含一个构造函数,访问用的函数,一个推进当前时间的函数adv ...

  2. Loadrunner关于页面检查的几个函数详解

    环境:Loadrunner版本:8.0自建一个test.html文件:<html><head><meta name="google1" content ...

  3. 【Linux】xshell连接中断后就无法连接虚拟机中的Linux

    具体情景是这样的: 在使用Linux的时候,本来一直好好的,突然就断了,我去百度了一番,网上的说法有千万种 有的说:是由于防火墙的问题 有的说:是由于Linux与其他ip冲突造成 ... 说法千万种, ...

  4. struts 在Action中访问web元素(request,session等)

    出发jsp: <?xml version="1.0" encoding="GB18030" ?> <%@ page language=&quo ...

  5. QT Unexpected CDB exit 问题的解决办法

    行QT进行debug时,提示 Unexpected CDB exit ,The CBD process terminated.. QtCreator 默认是没有调试器的,因此需要用户额外安装. win ...

  6. python list插入、拼接

    1可以使用"+"号完成操作 输出为: [1, 2, 3, 8, 'google', 'com'] 2.使用extend方法 . 输入相同 3使用切片 输出相同 PS:len(l1) ...

  7. asp.net,cookie,写cookie,取cookie(转载)

    Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一.Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一 ...

  8. void *指针的加减运算

    1.手工写了一个程序验证void *指针加减运算移动几个字节: //本程序验证空类型指针减1移动几个字节 #include <stdio.h> int main(int argc, cha ...

  9. U3D Debug.DrawRay

    Debug.DrawRay第二个参数方向,事实上需要手动输入长度.并且不是无限长的射线,是根据方向的长度来的 如果参数不指定颜色,绘制出来的就是白色

  10. Unity3D碰撞器事件测试(Rigidbody/Kinematic/Trigger/Collider)

    1.Kinematic和刚体之间的碰撞事件 Unity官方有一个详细的碰撞关系表:http://docs.unity3d.com/Manual/CollidersOverview.html 但其实可以 ...