Boost 是 C++ 社區最權威的開源庫集合,被譽為「C++ 標準的試驗場」。C++11/14/17/20 眾多新特性(如 shared_ptr, regex, thread, filesystem 等)均源自 Boost。
| 類別 | 代表庫 | 典型用途 |
|---|---|---|
| 字符串/文本 | Format, Regex, Tokenizer, Spirit | 類型安全格式化、正則、解析器生成 |
| 容器擴展 | Array, Bimap, Circular Buffer, MultiIndex | 固定大小數組、雙向映射、多索引容器 |
| 函數式/高階 | Bind, Function, Lambda, Phoenix | 函數對象綁定、Lambda 表達式(C++11 前) |
| 泛型編程 | TypeTraits, Concept Check, Enable If | Traits 技術、概念檢查、SFINAE 輔助 |
| 模板元編程 | MPL, Fusion, Proto | 編譯期計算、異構序列、DSL 構建 |
| 數值計算 | Multiprecision, Rational, Quaternion, Math Constants | 高精度、有理數、四元數、數學常數 |
| 測試/正確性 | Test, Static Assert, Contract | 單元測試框架、靜態斷言、契約式編程 |
| 數據結構 | Variant, Tuple, Any, Optional | 類型安全聯合體、元組、任意類型、可選值 |
| 跨語言 | Python, MPI | C++/Python 無縫互操作、並行消息傳遞 |
| 內存管理 | Pool, Smart Ptr, Intrusive | 內存池、智能指針、侵入式容器 |
| 雜項 | CRC, Date_Time, Filesystem, UUID | 校驗、日期時間、文件系統、唯一標識符 |
using namespace boost::lambda;
vector<int> v = {1,2,3};
for_each(v.begin(), v.end(), cout << _1 * 2 + 10 << "\n");
_1 為佔位符,代表當前元素,極具表達力。
namespace fs = boost::filesystem;
for (auto& p : fs::recursive_directory_iterator("."))
cout << p.path() << endl;
boost::noncopyable, shared_ptr, https://www.boost.org/doc/libs/,每庫有詳細教程。Boost.Outcome、Boost.Cobalt)。GUI(用 Qt/WxWidgets)、數據庫連接(用 SOCI/ODBC)、Web 框架(用 Crow/Drogon)等需尋找專門庫。