Boost 生态全景
Boost 是 C++ 最重要的第三方库集合,许多 Boost 组件最终成为 C++ 标准(如
shared_ptr、regex、filesystem、optional)。
Boost 核心组件速览
Boost 生态(按类别)
├── 智能指针(已入标准)
│ └── boost::shared_ptr → std::shared_ptr
│
├── 字符串与文本
│ ├── Boost.StringAlgo — 字符串算法
│ ├── Boost.Regex — 正则表达式
│ ├── Boost.Tokenizer — 字符串分割
│ └── Boost.Format — 格式化(已被 fmtlib 取代)
│
├── 容器
│ ├── Boost.Container — 额外容器(flat_map 等)
│ ├── Boost.MultiIndex — 多索引容器
│ ├── Boost.Bimap — 双向映射
│ └── Boost.CircularBuffer — 环形缓冲区
│
├── 网络与 I/O
│ ├── Boost.Asio — 异步 I/O(最重要)
│ └── Boost.Beast — HTTP/WebSocket(基于 Asio)
│
├── 并发
│ ├── Boost.Thread — 线程(已被 std::thread 取代)
│ ├── Boost.Fiber — 用户态协程
│ └── Boost.Lockfree — 无锁数据结构
│
├── 数学与算法
│ ├── Boost.Math — 数学函数
│ ├── Boost.Graph — 图算法
│ └── Boost.Geometry — 几何算法
│
└── 工具
├── Boost.Program_options — 命令行解析(已被 CLI11 取代)
├── Boost.Serialization — 序列化
├── Boost.Log — 日志(已被 spdlog 取代)
└── Boost.Test — 单元测试Boost.StringAlgo
cpp
#include <boost/algorithm/string.hpp>
std::string s = " Hello, World! ";
// 大小写
boost::to_upper(s);
boost::to_lower(s);
std::string upper = boost::to_upper_copy(s);
// 去除空白
boost::trim(s);
boost::trim_left(s);
boost::trim_right(s);
// 查找与替换
boost::replace_all(s, "World", "C++");
boost::replace_first(s, "l", "L");
std::string replaced = boost::replace_all_copy(s, " ", "_");
// 分割
std::vector<std::string> parts;
boost::split(parts, "a,b,c,d", boost::is_any_of(","));
// parts = {"a", "b", "c", "d"}
boost::split(parts, "hello world cpp",
boost::is_space(), boost::token_compress_on);
// 检查
boost::starts_with(s, "Hello");
boost::ends_with(s, "World");
boost::contains(s, "ello");
boost::iequals(s, "hello, world!"); // 忽略大小写比较
// 连接
std::vector<std::string> words = {"hello", "world", "cpp"};
std::string joined = boost::join(words, ", "); // "hello, world, cpp"Boost.MultiIndex
cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
namespace bmi = boost::multi_index;
struct Employee {
int id;
std::string name;
std::string dept;
double salary;
};
// 同时支持多种索引方式的容器
using EmployeeDB = bmi::multi_index_container<
Employee,
bmi::indexed_by<
bmi::ordered_unique<bmi::member<Employee, int, &Employee::id>>,
bmi::hashed_non_unique<bmi::member<Employee, std::string, &Employee::name>>,
bmi::ordered_non_unique<bmi::member<Employee, std::string, &Employee::dept>>
>
>;
EmployeeDB db;
db.insert({1, "Alice", "Engineering", 100000});
db.insert({2, "Bob", "Marketing", 80000});
db.insert({3, "Charlie", "Engineering", 90000});
// 按 id 查找(索引 0)
auto& by_id = db.get<0>();
auto it = by_id.find(1);
// 按 name 查找(索引 1)
auto& by_name = db.get<1>();
auto range = by_name.equal_range("Alice");
// 按 dept 范围查找(索引 2)
auto& by_dept = db.get<2>();
auto [lo, hi] = by_dept.equal_range("Engineering");
for (auto it = lo; it != hi; ++it) {
std::cout << it->name << "\n";
}Boost.Lockfree
cpp
#include <boost/lockfree/queue.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/lockfree/stack.hpp>
// 多生产者多消费者无锁队列
boost::lockfree::queue<int> queue(1024); // 容量 1024
// 生产者
queue.push(42);
// 消费者
int val;
if (queue.pop(val)) {
std::cout << val << "\n";
}
// 单生产者单消费者(更快)
boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024>> spsc;
spsc.push(42);
spsc.pop(val);
// 无锁栈
boost::lockfree::stack<int> stack(1024);
stack.push(42);
stack.pop(val);Boost.Beast(HTTP/WebSocket)
cpp
#include <boost/beast.hpp>
#include <boost/asio.hpp>
namespace beast = boost::beast;
namespace http = beast::http;
namespace asio = boost::asio;
using tcp = asio::ip::tcp;
// 同步 HTTP GET
asio::io_context io;
tcp::resolver resolver(io);
beast::tcp_stream stream(io);
auto results = resolver.resolve("example.com", "80");
stream.connect(results);
http::request<http::string_body> req{http::verb::get, "/", 11};
req.set(http::field::host, "example.com");
req.set(http::field::user_agent, "Boost.Beast");
http::write(stream, req);
beast::flat_buffer buf;
http::response<http::dynamic_body> res;
http::read(stream, buf, res);
std::cout << res.result_int() << "\n"; // 200
std::cout << beast::buffers_to_string(res.body().data()) << "\n";
stream.socket().shutdown(tcp::socket::shutdown_both);安装
bash
# Ubuntu
sudo apt install libboost-all-dev
# vcpkg(按需安装)
vcpkg install boost-algorithm boost-asio boost-beast boost-multi-index
# CMake
find_package(Boost REQUIRED COMPONENTS system filesystem)
target_link_libraries(myapp PRIVATE Boost::system Boost::filesystem)关键认知
Boost 是 C++ 标准的"孵化器",很多现代 C++ 特性来自 Boost。但现在很多 Boost 组件已有更好的替代:spdlog 替代 Boost.Log,CLI11 替代 Boost.Program_options,fmtlib 替代 Boost.Format。Boost.Asio 和 Boost.Beast 仍然是网络编程的首选。