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

1. char_separator

char_separator有两个构造函数
1. char_separator()
使用函数 std::isspace() 来识别被弃分隔符,同时使用 std::ispunct() 来识别保留分隔符。另外,抛弃空白单词。(见例2)
2. char_separator(// 不保留的分隔符
                               const Char* dropped_delims,
                               // 保留的分隔符
                               const Char* kept_delims = 0, 
                               // 默认不保留空格分隔符, 反之加上改参数keep_empty_tokens
                               empty_token_policy empty_tokens = drop_empty_tokens) 

该函数创建一个 char_separator 对象,该对象被用于创建一个 token_iterator 或 tokenizer 以执行单词分解。dropped_delims 和 kept_delims 都是字符串,其中的每个字符被用作分解时的分隔符。当在输入序列中遇到一个分隔符时,当前单词即完成,并开始下一个新单词。dropped_delims 中的分隔符不出现在输出的单词中,而 kept_delims 中的分隔符则会作为单词输出。如果 empty_tokens 为 drop_empty_tokens, 则空白单词不会出现在输出中。如果 empty_tokens 为 keep_empty_tokens 则空白单词将出现在输出中。 (见例3)

2. escaped_list_separator

escaped_list_separator有两个构造函数
下面三个字符做为分隔符: '\', ',', '"'
1. explicit escaped_list_separator(Char e = '\\', Char c = ',',Char q = '\"');

参数 描述
e 指定用作转义的字符。缺省使用C风格的\(反斜杠)。但是你可以传入不同的字符来覆盖它。
如果你有很多字段是Windows风格的文件名时,路径中的每个\都要转义。
你可以使用其它字符作为转义字符。
c 指定用作字段分隔的字符
q 指定用作引号的字符

2. escaped_list_separator(string_type e, string_type c, string_type q):

参数 描述
e 字符串e中的字符都被视为转义字符。如果给定的是空字符串,则没有转义字符。
c 字符串c中的字符都被视为分隔符。如果给定的是空字符串,则没有分隔符。
q 字符串q中的字符都被视为引号字符。如果给定的是空字符串,则没有引号字符。

3. offset_separator

offset_separator 有一个有用的构造函数
template<typename Iter>
  offset_separator(Iter begin,Iter end,bool bwrapoffsets = true, bool breturnpartiallast = true);

参数 描述
begin, end 指定整数偏移量序列
bwrapoffsets 指明当所有偏移量用完后是否回绕到偏移量序列的开头继续。
例如字符串 "1225200101012002" 用偏移量 (2,2,4) 分解,
如果 bwrapoffsets 为 true, 则分解为 12 25 2001 01 01 2002.
如果 bwrapoffsets 为 false, 则分解为 12 25 2001,然后就由于偏移量用完而结束。
breturnpartiallast 指明当被分解序列在生成当前偏移量所需的字符数之前结束,是否创建一个单词,或是忽略它。
例如字符串 "122501" 用偏移量 (2,2,4) 分解,
如果 breturnpartiallast 为 true,则分解为 12 25 01.
如果为 false, 则分解为 12 25,然后就由于序列中只剩下2个字符不足4个而结束。

例子

void test_string_tokenizer()  

{  

  using namespace boost;  

  // 1. 使用缺省模板参数创建分词对象, 默认把所有的空格和标点作为分隔符.   

    {  

    std::string str("Link raise the master-sword.");  

    tokenizer<> tok(str);  

    for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  

            std::cout << "[" << *pos << "]";  

        std::cout << std::endl;  

    // [Link][raise][the][master][sword]  

    }  

  // 2. char_separator()  

    {  

    std::string str("Link raise the master-sword.");  

    // 一个char_separator对象, 默认构造函数(保留标点但将它看作分隔符)  

    char_separator<char> sep;  

    tokenizer<char_separator<char> > tok(str, sep);  

    for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  

      std::cout << "[" << *pos << "]";  

    std::cout << std::endl;  

    // [Link][raise][the][master][-][sword][.]  

    }  

    // 3. char_separator(const Char* dropped_delims,  

    //                   const Char* kept_delims = 0,   

    //                   empty_token_policy empty_tokens = drop_empty_tokens)  

    {  

      std::string str = ";!!;Hello|world||-foo--bar;yow;baz|";  

      char_separator<char> sep1("-;|");  

      tokenizer<char_separator<char> > tok1(str, sep1);  

      for (BOOST_AUTO(pos, tok1.begin()); pos != tok1.end(); ++pos)  

        std::cout << "[" << *pos << "]";  

      std::cout << std::endl;  

      // [!!][Hello][world][foo][bar][yow][baz]  

      char_separator<char> sep2("-;", "|", keep_empty_tokens);  

      tokenizer<char_separator<char> > tok2(str, sep2);  

      for (BOOST_AUTO(pos, tok2.begin()); pos != tok2.end(); ++pos)  

        std::cout << "[" << *pos << "]";  

      std::cout << std::endl;  

      // [][!!][Hello][|][world][|][][|][][foo][][bar][yow][baz][|][]  

    }  

  // 4. escaped_list_separator  

    {  

      std::string str = "Field 1,\"putting quotes around fields, allows commas\",Field 3";  

      tokenizer<escaped_list_separator<char> > tok(str);  

      for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  

        std::cout << "[" << *pos << "]";  

      std::cout << std::endl;  

   // [Field 1][putting quotes around fields, allows commas][Field 3]  

  // 引号内的逗号不可做为分隔符.  

    }  

  // 5. offset_separator  

    {  

      std::string str = "";  

      int offsets[] = {, , };  

      offset_separator f(offsets, offsets + );  

      tokenizer<offset_separator> tok(str, f);  

      for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  

        std::cout << "[" << *pos << "]";  

      std::cout << std::endl;  

    }  

}  

boost::tokenizer详解的更多相关文章

  1. 【Boost】boost::tokenizer详解

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

  2. Boost 安装详解

    一 Linux(redhat)篇 1.1 获取boost库 解压tar -zxvf boost_1.48.0.tar.gz 进入解压目录cd boost_1_48_0 1.2 编译安装 使用下面的命令 ...

  3. 【Boost】boost::string_algo详解2——find相关函数

    来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明:   template<typename Range1T, typ ...

  4. Boost线程详解

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

  5. boost::bind 详解

    使用 boost::bind是标准库函数std::bind1st和std::bind2nd的一种泛化形式.其可以支持函数对象.函数.函数指针.成员函数指针,并且绑定任意参数到某个指定值上或者将输入参数 ...

  6. boost/config.hpp文件详解

    简要概述 今天突发奇想想看一下boost/config.hpp的内部实现,以及他有哪些功能. 这个头文件都有一个类似的结构,先包含一个头文件,假设为头文件1,然后包含这个头文 件中定义的宏.对于头文件 ...

  7. Linux下boost库的编译、安装详解

    下载boost源码 boost下载地址 解压到一个目录 tar -zxvf boost_1_66_0.tar.gz 编译boost库 进入boost_1_66_0目录中 cd boost_1_66_0 ...

  8. Boost::split用法详解

    工程中使用boost库:(设定vs2010环境)在Library files加上 D:\boost\boost_1_46_0\bin\vc10\lib在Include files加上 D:\boost ...

  9. Solr部署详解

    Solr部署详解 时间:2013-11-24 方式:转载 目录 1 solr概述 1.1 solr的简介 1.2 solr的特点 2 Solr安装 2.1 安装JDK 2.2 安装Tomcat 2.3 ...

随机推荐

  1. kafka命令使用

    1.创建 topic /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 - ...

  2. 【.net】在ASP.NET中,IE与Firefox下载文件名中带中文汉字的文件,文件名乱码的问题

    #问题:客户端为ie或Firefox,服务端为asp.net时,下载文件名中包含中文汉字时,下载下来的文件的文件名是乱码: #解决方案: 示例代码:下载名称中带汉字的文件: public void P ...

  3. HDFS 读写数据流程

    一.上传数据 二.下载数据 三.读写时的节点位置选择 1.网络节点距离(机架感知) 下图中: client 到 DN1 的距离为 4 client 到 NN 的距离为 3 DN1 到 DN2 的距离为 ...

  4. Hbase记录-hbase部署

    #版本支持 #官网下载二进制包,解压到/usr/app下,配置/etc/profile: export HBASE_HOME=/usr/app/hbase export PATH=$HBASE_HOM ...

  5. canvas加载图片需要二次刷新的问题

    如题:此问题我也经在百度问问上进行了解答.https://zhidao.baidu.com/question/1048045241465845579.html 好吧,难怪现在百度那么坑人,理论水军专家 ...

  6. yum方式安装mysql

    安装步骤 1. 安装yum仓库 到https://dev.mysql.com/downloads/repo/yum/ 下载对应操作系统版本的yum仓库,然后进行安装:sudo rpm -Uvh pla ...

  7. vue 移动端日期选择组件 vue-mobile-calendar

    vue-mobile-calendar cnpm install vue-mobile-calendar -S import Vue from 'vue' import Calendar from ' ...

  8. 【LeetCode】108. Convert Sorted Array to Binary Search Tree

    Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...

  9. 【游记】关于NOIP2017

    -2017.11.13- Day0.到达酒店无所事事.跟着两个大佬拉着窗帘玩恐怖游戏留下了心理阴影,然后跑去找葱葱一起复习.晚上很晚才睡.Day1.T1结论题,以前写过.T2模拟,细节有点多.T3Di ...

  10. (15)DeleteColumnsMakeSortedIII

    一.问题描述 给定一个字符串形的数组,求最小的删除数目,使得删除后的字符串是字典型有序的. 二.思路Code package algorithm; /** * Created by adrian.wu ...