Boost Tokenizer 使用介绍
来源:互联网 发布:软件家园 编辑:程序博客网 时间:2024/05/16 14:40
Boost Tokenizer 使用介绍
-------------------------1. 介绍
Boost Tokenizer提供了一种把字符序列转换成一组Token的能力,当然,你也可以定义TokenizerFunction来自定义序列的切分符号,如果不指定,默认是以空格为分割,去掉一些标点符号。2. 几个简单的例子
下面是一个简单的例子:
// simple_example_1.cpp#include<iostream>#include<boost/tokenizer.hpp>#include<string>int main(){ using namespace std; using namespace boost; string s = "This is, a test"; tokenizer<> tok(s); for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){ cout << *beg << "\n"; }}
结果如下:
This
is
a
test
这里已经过滤了标点符号。
下面是一个以字符步长来进行分割的例子:
// simple_example_3.cpp#include<iostream>#include<boost/tokenizer.hpp>#include<string>int main(){ using namespace std; using namespace boost; string s = "12252001"; int offsets[] = {2,2,4}; // 这里指定了三个步长 offset_separator f(offsets, offsets+3); tokenizer<offset_separator> tok(s,f); for(tokenizer<offset_separator>::iterator beg=tok.begin(); beg!=tok.end();++beg){ cout << *beg << "\n"; }}
结果如下:
12
23
2001
3. 什么是TokenizerFunction
TokenizerFunction是一个用于查询符合匹配要求的token,目前提供了三种TokenizerFunction模板,× escaped_list_separator 主要用于解析csv格式的字符串
explicit escaped_list_separator(Char e = '\\', Char c = ',',Char q = '\"')
escaped_list_separator(string_type e, string_type c, string_type q):
× offset_separator 主要用于解析基于特定步长的要求
template<typename Iter>
× char_separator 主要是用于解析基于特定字符分割的需求offset_separator(Iter begin,Iter end,bool bwrapoffsets = true, bool breturnpartiallast = true)
explicit char_separator(const Char* dropped_delims,const Char* kept_delims = "",
empty_token_policy empty_tokens = drop_empty_tokens)
4. 一个简单的解析/etc/passwd的例子
/** * @auth lemo.lu * @date 2011.11.03 * * example of Boost tokenizer template usage,This example uses delimiter * separator. */// stl header#include <iostream> // iostream#include <string> // string#include <fstream> // ifstream// boost#include <boost/tokenizer.hpp> // boost Tokenizerint main(){ std::ifstream passwdFile; passwdFile.open("/etc/passwd",std::ifstream::in); // store password line char passwdString[256]; typedef boost::tokenizer<boost::char_separator<char> > passwdTokenizer; // set a TokenizerFunction , dropped delimiters ":" and keep delimiters "" boost::char_separator<char> tokenSep(":", "", boost::keep_empty_tokens); // passwd format information static const char* passwd_st[] = { "Account","password","UID","GID","GECOS","Dir","Shell" }; // iterator the passwd file while(passwdFile.good()) { // get line passwdFile.getline(passwdString,256); passwdTokenizer tok(std::string(passwdString), tokenSep); int passwd_c = 0; for(passwdTokenizer::iterator curTok=tok.begin(); curTok!=tok.end(); ++curTok) std::cout << passwd_st[passwd_c++] << ":" << *curTok << std::endl; std::cout << "---------------------" << std::endl; }passwdFile.close();}
部分结果如下:
Account:root
password:x
UID:0
GID:0
GECOS:root
Dir:/root
Shell:/bin/bash
---------------------
Account:daemon
password:x
UID:1
GID:1
GECOS:daemon
Dir:/usr/sbin
Shell:/bin/sh
---------------------
5. 参考
http://www.boost.org/doc/libs/1_47_0/libs/tokenizer/- Boost Tokenizer 使用介绍
- boost tokenizer
- boost tokenizer
- boost::tokenizer
- boost tokenizer
- 使用boost/tokenizer对字符串解析拆分
- boost库之tokenizer的使用
- 【Boost】boost::tokenizer详解
- Boost tokenizer examples
- [转载]boost tokenizer
- Boost学习笔记-tokenizer
- boost之Tokenizer
- boost::tokenizer详解
- Boost学习笔记-tokenizer
- boost::tokenizer分词器
- boost tokenizer 坑
- 用boost::tokenizer分割字符串
- Boost之字符串分割器-Tokenizer
- 如何把程序钉到Windows7任务栏
- window.event对象详解
- 二叉树
- android中socket编程
- Android高手进阶教程(六)之----Android 中MenuInflater的使用(布局定义菜单)!
- Boost Tokenizer 使用介绍
- 获取鼠标的坐标
- linux 中的top命令下的VIRT RES
- 安装系统出现0x80070017错误
- 离线处理
- 插入排序方式
- Android高手进阶教程(七)之----Android 中Preferences的使用!
- 如何在地图上画线路 iphone开发
- 买单,Square,记在我的账上!