稳定版本 | 1.77.0 (2021年8月11日 ) |
---|---|
类型 | 库 |
许可协议 | Boost许可证 |
网站 | www |
Boost C++ 库(Libraries)是一组扩展C++功能的经过同行评审(Peer-reviewed)且开放源码程序库。大多数的函数为了能够以开放源码或者封闭项目的方式运作,而许可于Boost软件许可协议(Boost Software License)之下。许多Boost的开发人员是来自C++标准委员会,而部分的Boost库成为C++的TR1标准之一。[1]
为了要确保库的效率与弹性,Boost广泛的使用模板(template)功能。而它是针对各式领域的C++用户与应用领域(Application Domain)上,包含的库类别从像smart_ptr 库这种类通用库,到像是文件系统的操作系统抽象层,甚至能够利用Boost来开发额外的库或是给高级的C++用户利用,像是MPL。
内容
- 算法
- 并行计算
- 容器
- array - STL的数组容器
- Boost Graph Library (BGL) - 通用的图容器,组件和算法
- multi-array - N维数组
- multi-index containers - 多索引容器
- pointer containers - 指针容器
- property map - 属性Map
- variant - 安全的,基于泛型的,支持访问者模式的联合
- fusion - 基于tuple的容器和算法集合
- 正当性与测试
- concept check - 检查模板参数是否满足模板的要求
- static assert - 编译期的断言检查
- Boost Test Library - C++ 单元测试框架
- 数据结构
- dynamic_bitset -
std::bitset-
的动态转型
- dynamic_bitset -
- 仿函数与高端函数(含无名关数)
- 泛型
- 图
- I/O
- 语言之间的支持(Python用)
- 迭代器
- 数学和计算
- 内存(memory)
- pool - 内存池,boost提供4种内存池模型供使用:pool、object_pool、singleton_pool、pool_allocator/fast_pool_allocator
- smart_ptr - boost的smart_ptr中提供了4种智能指针,作为std::auto_ptr的补充
- scoped_ptr - 具作用域指针,与std::auto_ptr类似,但不能转让所有权,用于确保离开作用域能够正确地删除动态分配的对象
- scoped_array - 配合scoped_ptr使用
- shared_ptr -
- shared_array - 配合shared_ptr使用
- weak_ptr - shared_ptr 的观察者,避免shared_ptr循环引用,是一种辅助指针
- intrusive_ptr - 比 shared_ptr 更好的智能指针
- utility - 以下是utility类型的定义。
- base from member idiom -
- checked delete - 保证在摧毁一个对象时,必须对该对象的类型有充份了解
- next and prior functions -
- noncopyable - 把copy constructor和assign operaotr 宣告为private,不加以实现
- addressof - 用于获得变量的地址
- result_of - 指涉函数回返类型
- 序列化
- 语法分析器
- 预处理元编程
- 字符串与文字处理(正则表达式等)
- lexical_cast - lexical_cast 类别模板
- format - 文字格式化,类似printf
- iostreams - 新式iostream的补强
- regex - 正规表示法(Regular expression)
- Spirit - 根据EBNF规则对文件进行分析
- string algorithms - 文字列算法
- tokenizer - 把字符串序列分解成一系列标记(tokens)
- wave -
- 模板元编程(Template Metaprogramming)
- mpl - 模板元编程框架
- static assert - 静态断言
- type traits - 类型的基本属性的模板
- 其他
范例
现有的 Boost 包含大约150种不同的函数库,以下面几项做范例:
线性代数 – uBLAS
Boost 包含了 uBLAS 线性代数函数库,能够借由基本函数库子函数(BLAS)来支持向量与矩阵形运算。
- 此范例表示如何矩阵与向量作乘积:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <iostream>
using namespace boost::numeric::ublas;
/* 举例 "y = Ax" */
int main ()
{
vector<double> x (2);
x(0) = 1; x(1) = 2;
matrix<double> A(2,2);
A(0,0) = 0; A(0,1) = 1;
A(1,0) = 2; A(1,1) = 3;
vector<double> y = prod(A, x);
std::cout << y << std::endl;
return 0;
}
随机数产生 – Boost.Random
Boost 也提供独立分布的模拟随机与 PRNG 独立性的机率分布,而这些能够具体的创建产生器。
- 此范例表示如何使用 Mersenne Twister 算法来产生随机
#include <boost/random.hpp>
#include <ctime>
using namespace boost;
double SampleNormal (double mean, double sigma)
{
// 建立一个 Mersenne twister 随机数产生器
// 使用 Unix 时间设定 seed
static mt19937 rng(static_cast<unsigned> (std::time(0)));
// 选择高斯机率分布
normal_distribution<double> norm_dist(mean, sigma);
// 使用 function 的形式,生成随机数据产生器
variate_generator<mt19937&, normal_distribution<double> > normal_sampler(rng, norm_dist);
// 传回样本分布结果
return normal_sampler();
}
更详细的说明请参阅 Boost 随机数库。
多线程 – Boost.Thread
示例码演示创建线程:
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
void hello_world()
{
cout << "Hello world, I'm a thread!" << endl;
}
int main(int argc, char* argv[])
{
// 開始一條使用 "hello_world" function 的新執行緒
boost::thread my_thread(&hello_world);
// 等待執行緒完成工作
my_thread.join();
return 0;
}
- Introduction to Boost.Threads in Dr. Dobb's Journal. (2002)
- Boost.Threads API reference。
- threadpool library based on Boost.Thread
引用
- ↑ Library Technical Report. [2008-08-07].
外部链接
- Boost 官方网站
- Smart Pointers to boost your code
- Building Boost libraries
- Boost 中文翻译 在线阅读