【Boost】boost::tokenizer詳解

來源:互聯網
上載者:User

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中的字元都被視為逸出字元。如果給定的是Null 字元串,則沒有逸出字元。
c 字串c中的字元都被視為分隔字元。如果給定的是Null 字元串,則沒有分隔字元。
q 字串q中的字元都被視為引號字元。如果給定的是Null 字元串,則沒有引號字元。

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 = "12252001400";int offsets[] = {2, 2, 4};offset_separator f(offsets, offsets + 3);tokenizer<offset_separator> tok(str, f);for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)std::cout << "[" << *pos << "]";std::cout << std::endl;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.