網站首頁 編程語言 正文
介紹
庫 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
相關推薦
- 2022-08-25 高級消息隊列協議AMQP簡介_其它綜合
- 2022-05-11 python?DataFrame的shift()方法的使用_python
- 2022-03-26 聊聊virtualbox6安裝centos增強功能問題_VirtualBox
- 2022-06-14 go語言中的udp協議及TCP通訊實現示例_Golang
- 2024-02-29 UNI-APP獲取當前位置,出現getLocation:fail [geolocation:7]錯誤
- 2022-09-03 C#中DataSet、DataTable、DataRow數據的復制方法_C#教程
- 2022-05-23 C++單例模式的幾種實現方法詳解_C 語言
- 2023-06-13 C++?ncnn模型驗證精度實現代碼_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支