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. 算法笔记_061:蓝桥杯练习 字串统计(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 给定一个长度为n的字符串S,还有一个数字L,统计长度大于等于L的出现次数最多的子串(不同的出现可以相交),如果有多个,输出最长的,如果仍然 ...

  2. ionic 项目使用百度地图插件(cordova-qdc-baidu-location)

    现在我们使用'Weizhe He'提供的cordova-qdc-baidu-location来尝试创建简单的定位app. Stpe1:创建一个项目 Stpe2:申请百度地图API秘钥     应用类型 ...

  3. 一次vm 虚拟机时间倒流而导致的oracle 数据库启动故障

    一次vm 虚拟机时间倒流而导致的oracle 数据库启动故障 本文是原创文章.若转载请注明出处: http://blog.csdn.net/msdnchina/article/details/3878 ...

  4. Python进阶---python strip() split()函数实战(转)

    先看一个例子: >>> ipaddr = 10.122.19.10 File "", line 1 ipaddr = 10.122.19.10 ^ SyntaxE ...

  5. 误删 libc.so.6的解决方法(转)

    今天不小心把libc.so.6给删除了. 原系统是这样的: libc.so.6 -> lib-2.6.1.so 本想新建立一个软链接,指向 lib-2.8.so 没想到 ln 命令不能用了. 原 ...

  6. Hibernate HQL详解

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5606444.html  1.实体查询: hql="FROM ...

  7. (四)Oracle学习笔记—— 常见函数

    1. 字符串类型及函数 字符类型分 种,char(n) .varchar(n).varchar2(n) : char(n)固定长度字符串,假如长度不足 n,右边空格补齐: varchar(n)可变长度 ...

  8. http://blog.sina.com.cn/s/blog_6a01140c0100wimi.html

    http://blog.sina.com.cn/s/blog_6a01140c0100wimi.html

  9. SGA 的自动管理

    在Oracle10g中,不必再如从前一样用下列各个参数分别指定SGA的每个部分的大小.也就是说不需要首先评估SGA各组件的大小,并且在init<SID>.ora初始参数文件中分组件指定.( ...

  10. 要练习的lambda

    取list的id 放入新List<Integer> List<Integer> list1 = list.stream().map(albumGroup1 -> albu ...