【Boost】boost::tokenizer详解

 

 

分类: [C++]--[Boost]
 

目录(?)[+]

 

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个而结束。

例子

  1. void test_string_tokenizer()  
  2. {  
  3.     using namespace boost;  
  4.   
  5.     // 1. 使用缺省模板参数创建分词对象, 默认把所有的空格和标点作为分隔符.   
  6.     {  
  7.         std::string str("Link raise the master-sword.");  
  8.   
  9.         tokenizer<> tok(str);  
  10.         for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  
  11.             std::cout << "[" << *pos << "]";  
  12.         std::cout << std::endl;  
  13.         // [Link][raise][the][master][sword]  
  14.     }  
  15.   
  16.     // 2. char_separator()  
  17.     {  
  18.         std::string str("Link raise the master-sword.");  
  19.   
  20.         // 一个char_separator对象, 默认构造函数(保留标点但将它看作分隔符)  
  21.         char_separator<char> sep;  
  22.         tokenizer<char_separator<char> > tok(str, sep);  
  23.         for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  
  24.             std::cout << "[" << *pos << "]";  
  25.         std::cout << std::endl;  
  26.         // [Link][raise][the][master][-][sword][.]  
  27.     }  
  28.   
  29.     // 3. char_separator(const Char* dropped_delims,  
  30.     //                   const Char* kept_delims = 0,   
  31.     //                   empty_token_policy empty_tokens = drop_empty_tokens)  
  32.     {  
  33.         std::string str = ";!!;Hello|world||-foo--bar;yow;baz|";  
  34.   
  35.         char_separator<char> sep1("-;|");  
  36.         tokenizer<char_separator<char> > tok1(str, sep1);  
  37.         for (BOOST_AUTO(pos, tok1.begin()); pos != tok1.end(); ++pos)  
  38.             std::cout << "[" << *pos << "]";  
  39.         std::cout << std::endl;  
  40.         // [!!][Hello][world][foo][bar][yow][baz]  
  41.   
  42.         char_separator<char> sep2("-;", "|", keep_empty_tokens);  
  43.         tokenizer<char_separator<char> > tok2(str, sep2);  
  44.         for (BOOST_AUTO(pos, tok2.begin()); pos != tok2.end(); ++pos)  
  45.             std::cout << "[" << *pos << "]";  
  46.         std::cout << std::endl;  
  47.         // [][!!][Hello][|][world][|][][|][][foo][][bar][yow][baz][|][]  
  48.     }  
  49.   
  50.     // 4. escaped_list_separator  
  51.     {  
  52.         std::string str = "Field 1,\"putting quotes around fields, allows commas\",Field 3";  
  53.   
  54.         tokenizer<escaped_list_separator<char> > tok(str);  
  55.         for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  
  56.             std::cout << "[" << *pos << "]";  
  57.         std::cout << std::endl;  
  58.         // [Field 1][putting quotes around fields, allows commas][Field 3]  
  59.         // 引号内的逗号不可做为分隔符.  
  60.     }  
  61.       
  62.     // 5. offset_separator  
  63.     {  
  64.         std::string str = "12252001400";  
  65.   
  66.         int offsets[] = {2, 2, 4};  
  67.         offset_separator f(offsets, offsets + 3);  
  68.         tokenizer<offset_separator> tok(str, f);  
  69.   
  70.         for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)  
  71.             std::cout << "[" << *pos << "]";  
  72.         std::cout << std::endl;  
  73.     }  
  74. }  
posted @ 2015-03-26 18:10  廖先生  阅读(597)  评论(0编辑  收藏  举报