日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++?Boost?Tokenizer使用詳細講解_C 語言

作者:無水先生 ? 更新時間: 2022-12-11 編程語言

介紹

庫 Boost.Tokenizer 允許您通過將某些字符解釋為分隔符來迭代字符串中的部分表達式。使用 boost::tokenizer 迭代字符串中的部分表達式

示例一

使用 boost::tokenizer 迭代字符串中的部分表達式

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  std::string s = "Boost C++ Libraries";
  tokenizer tok{s};
  for (tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
    std::cout << *it << '\n';
}

Boost.Tokenizer 在 boost/tokenizer.hpp 中定義了一個名為 boost::tokenizer 的類模板。它期望一個標識連貫表達式的類作為模板參數。示例 10.1 使用了 boost::char_separator 類,它將空格和標點符號解釋為分隔符。

必須使用 std::string 類型的字符串初始化標記器。使用成員函數 begin() 和 end(),可以像容器一樣訪問標記器。用于初始化標記器的字符串的部分表達式可通過迭代器獲得。部分表達式的計算方式取決于作為模板參數傳遞的類的類型。

因為 boost::char_separator 默認將空格和標點符號解釋為分隔符,所以示例 10.1 會顯示 Boost、C、+、+ 和庫。 boost::char_separator 使用 std::isspace() 和 std::ispunct() 來識別分隔符。 Boost.Tokenizer 區分應該顯示的分隔符和應該抑制的分隔符。默認情況下,空格被抑制并顯示標點符號。

示例二

初始化 boost::char_separator 以適應迭代

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  std::string s = "Boost C++ Libraries";
  boost::char_separator<char> sep{" "};
  tokenizer tok{s, sep};
  for (const auto &t : tok)
    std::cout << t << '\n';
}

為了防止標點符號被解釋為分隔符,請在將 boost::char_separator 對象傳遞給分詞器之前對其進行初始化。

boost::char_separator 的構造函數一共接受三個參數,但只需要第一個。第一個參數描述被抑制的各個分隔符。示例 10.2 與示例 10.1 一樣,將空格視為分隔符。

第二個參數指定應顯示的分隔符。如果省略此參數,則不顯示分隔符,程序現在將顯示 Boost、C++ 和庫。

示例三

使用 boost::char_separator 模擬默認行為

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  std::string s = "Boost C++ Libraries";
  boost::char_separator<char> sep{" ", "+"};
  tokenizer tok{s, sep};
  for (const auto &t : tok)
    std::cout << t << '\n';
}

如果將加號作為第二個參數傳遞,則示例 10.3 的行為類似于示例 10.1。

第三個參數決定是否顯示空的部分表達式。如果連續找到兩個分隔符,則對應的部分表達式為空。默認情況下,不顯示這些空表達式。使用第三個參數,可以更改默認行為。

示例四

初始化 boost::char_separator 以顯示空的部分表達式

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
  std::string s = "Boost C++ Libraries";
  boost::char_separator<char> sep{" ", "+", boost::keep_empty_tokens};
  tokenizer tok{s, sep};
  for (const auto &t : tok)
    std::cout << t << '\n';
}

示例 10.4 顯示了兩個額外的空部分表達式。第一個位于兩個加號之間,而第二個位于第二個加號和后面的空格之間。

示例五

具有寬字符串的 Boost.Tokenizer

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::char_separator<wchar_t>,
    std::wstring::const_iterator, std::wstring> tokenizer;
  std::wstring s = L"Boost C++ Libraries";
  boost::char_separator<wchar_t> sep{L" "};
  tokenizer tok{s, sep};
  for (const auto &t : tok)
    std::wcout << t << '\n';
}

Example

示例 10.5 迭代一個 std::wstring 類型的字符串。為了支持此字符串類型,必須使用附加模板參數初始化標記器。類 boost::char_separator 也必須用 wchar_t 初始化。

除了 boost::char_separator 之外,Boost.Tokenizer 還提供了兩個額外的類來識別部分表達式。

示例六

使用 boost::escaped_list_separator 解析 CSV 文件

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;
  std::string s = "Boost,\"C++ Libraries\"";
  tokenizer tok{s};
  for (const auto &t : tok)
    std::cout << t << '\n';
}

boost::escaped_list_separator 用于讀取以逗號分隔的多個值。這種格式通常稱為 CSV(逗號分隔值)。 boost::escaped_list_separator 還處理雙引號和轉義序列。因此,示例 10.6 的輸出是 Boost 和 C++ 庫。

提供的第二個類是 boost::offset_separator,它必須被實例化。相應的對象必須作為第二個參數傳遞給 boost::tokenizer 的構造函數。

示例七

使用 boost::offset_separator 迭代部分表達式

#include <boost/tokenizer.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::tokenizer<boost::offset_separator> tokenizer;
  std::string s = "Boost_C++_Libraries";
  int offsets[] = {5, 5, 9};
  boost::offset_separator sep{offsets, offsets + 3};
  tokenizer tok{s, sep};
  for (const auto &t : tok)
    std::cout << t << '\n';
}

boost::offset_separator 指定字符串中各個部分表達式結束的位置。示例 10.7 指定第一個部分表達式在 5 個字符后結束,第二個在另外 5 個字符后結束,第三個在以下 9 個字符后結束。輸出將是 Boost、_C++_ 和庫。

原文鏈接:https://yamagota.blog.csdn.net/article/details/127298695

欄目分類
最近更新