Boost (C++函数库)

Boost C++ 函式庫(Libraries)是一組擴充C++功能的經過同行評審(Peer-reviewed)且開放源碼程式庫。大多數的函式為了能夠以開放源碼或者封閉專案的方式運作,而授權於Boost軟體授權條款(Boost Software License)之下。許多Boost的開發人員是來自C++標準委員會,而部份的Boost函式庫成為C++的TR1標準之一。

為了要確保函式庫的效率與彈性,Boost廣泛的使用C++模板功能。而它是針對各式領域的C++使用者與應用領域(Application Domain)上,包含的函式庫類別從smart_ptr這種類通用函式庫,到boost-filesystem这种檔案系統的作業系統抽象層,甚至能夠利用Boost來開發額外的函式庫或是給進階的C++使用者利用,例如MPL。

內容
*演算法
*并行计算
**[http://boost.org/libs/thread/index.html thread] - 執行緒
**[http://boost.org/libs/context/index.html context] - 用戶層級上下文交換
*容器
**[http://boost.org/doc/html/array.html array] - STL的数组容器
**[http://boost.org/libs/graph/doc/table_of_contents.html Boost Graph Library (BGL)] - 通用的图容器,组件和算法
**[http://boost.org/libs/multi_array/doc/index.html multi-array] - N维数组
**[http://boost.org/libs/multi_index/doc/index.html multi-index containers] - 多索引容器
**[http://boost.org/libs/ptr_container/doc/ptr_container.html pointer containers] - 指针容器
**[https://web.archive.org/web/20081010133941/http://www.boost.org/libs/property_map/property_map.html property map] - 属性Map
**[http://boost.org/doc/html/variant.html variant] - 安全的,基于泛型的,支持访问者模式的联合
**[http://boost.org/libs/fusion/ fusion] - 基于tuple的容器和算法集合
*正当性與測試
**[http://boost.org/libs/concept_check/concept_check.htm concept check] - 檢查模板參數是否滿足模板的要求
**[http://boost.org/doc/html/boost_staticassert.html static assert] - 編譯期的斷言檢查
**[https://web.archive.org/web/20080516051646/http://boost.org/libs/test/doc/index.html Boost Test Library] - C++ 单元测试框架
*資料結構
**[http://boost.org/libs/dynamic_bitset/ dynamic_bitset] - std::bitset-的動態轉型
*仿函式與高階函式(含無名関数)
**[http://boost.org/libs/bind/bind.html bind] and [http://www.boost.org/libs/bind/mem_fn.html mem_fn] - 函式的綁定
**[http://boost.org/doc/html/function.html function] - 函式。
**[http://boost.org/libs/functional/index.html functional] - C++標準函式之強化。包含以下的内容。
***[http://boost.org/libs/functional/function_traits.html function object traits]
***[http://boost.org/libs/functional/negators.html negators]
***[http://boost.org/libs/functional/binders.html binders]
***[http://boost.org/libs/functional/ptr_fun.html adapters for pointers to functions]
***[http://boost.org/libs/functional/mem_fun.html adapters for pointers to member functions]
**[http://boost.org/doc/html/hash.html hash] - C++ Technical Report 1(TR1)定義的雜湊表
**[http://boost.org/doc/html/lambda.html lambda] - λ演算的實作
**[http://boost.org/doc/html/ref.html ref] - 標準C++参照(call by reference)的加強、特別強化與函式的呼叫
**[http://boost.org/libs/utility/utility.htm#result_of result_of] - 函式型別與回傳值
**[http://boost.org/doc/html/signals2.html signals2] - 信号和槽回调的实现托管
*泛型
*圖
*I/O
*語言之間的支援(Python用)
*迭代器
**[http://boost.org/libs/iterator/doc/index.html iterators]
**[http://boost.org/libs/utility/operators.htm operators]
**[http://boost.org/libs/tokenizer/index.html tokenizer]
*数学和計算
*内存(memory)
**[http://boost.org/libs/pool/doc/index.html pool] - 内存池,boost提供4种内存池模型供使用:pool、object_pool、singleton_pool、pool_allocator/fast_pool_allocator
**[http://boost.org/libs/smart_ptr/smart_ptr.htm smart_ptr] - boost的smart_ptr中提供了4种智能指针,作为std::auto_ptr的补充
***[https://web.archive.org/web/20080905004740/http://www.boost.org/libs/smart_ptr/scoped_ptr.htm scoped_ptr] - 具作用域指针,与std::auto_ptr类似,但不能转让所有权,用于确保离开作用域能夠正确地删除动态分配的对象
***[https://web.archive.org/web/20080908033515/http://boost.org/libs/smart_ptr/scoped_array.htm scoped_array] - 配合scoped_ptr使用
***[https://web.archive.org/web/20081011105952/http://www.boost.org/libs/smart_ptr/shared_ptr.htm shared_ptr] -
***[https://web.archive.org/web/20080905004024/http://www.boost.org/libs/smart_ptr/shared_array.htm shared_array] - 配合shared_ptr使用
***[https://web.archive.org/web/20080724032255/http://boost.org/libs/smart_ptr/weak_ptr.htm weak_ptr] - shared_ptr 的观察者,避免shared_ptr循环引用,是一种辅助指针
***[https://web.archive.org/web/20081010135940/http://www.boost.org/libs/smart_ptr/intrusive_ptr.html intrusive_ptr] - 比 shared_ptr 更好的智能指针
**[http://boost.org/libs/utility/utility.htm utility] - 以下是utility类型的定义。
***[https://web.archive.org/web/20081204140532/http://www.boost.org/libs/utility/base_from_member.html base from member idiom] -
***[http://boost.org/libs/utility/checked_delete.html checked delete] - 保证在摧毀一个对象时,必须对该对象的类型有充份了解
***[http://boost.org/libs/utility/utility.htm#functions_next_prior next and prior functions] -
***[http://boost.org/libs/utility/utility.htm#Class_noncopyable noncopyable] - 把copy constructor和assign operaotr 宣告为private,不加以实现
***[http://boost.org/libs/utility/utility.htm#addressof addressof] - 用于获得变量的地址
***[http://boost.org/libs/utility/utility.htm#result_of result_of] - 指涉函式回返型別
*序列化
*語法分析器
*預處理元編程
*字串與文字處理(正規表示式等)
**[https://web.archive.org/web/20080724032825/http://boost.org/libs/conversion/lexical_cast.htm lexical_cast] - lexical_cast 类別模板
**[http://boost.org/libs/format/index.html format] - 文字格式化,类似printf
**[http://boost.org/libs/iostreams/doc/index.html iostreams] - 新式iostream的補強
**[https://web.archive.org/web/20080113211048/http://www.boost.org/libs/regex/doc/index.html regex] - 正規表示法(Regular expression)
** - 根据EBNF规则对文件进行分析
**[http://boost.org/doc/html/string_algo.html string algorithms] - 文字列演算法
**[http://boost.org/libs/tokenizer/index.html tokenizer] - 把字串序列分解成一系列标记(tokens)
**[http://boost.org/libs/wave/index.html wave] -
*模板元编程(Template Metaprogramming)
**[http://boost.org/libs/mpl/doc/index.html mpl] - 模板元编程框架
**[http://boost.org/doc/html/boost_staticassert.html static assert] - 靜態斷言
**[https://web.archive.org/web/20080113213705/http://boost.org/doc/html/boost_typetraits.html type traits] - 型別的基本属性的模板
*其他

范例
现有的 Boost 包含大约150种不同的函数库,以下面几项做范例:

线性代数 – uBLAS
Boost 包含了 uBLAS 线性代数函数库,能够藉由基本函数库子函数(BLAS)来支持向量与矩阵形运算。

  • 此范例表示如何矩阵与向量作乘积:

#include
#include
#include
#include

using namespace boost::numeric::ublas;

/ 举例 "y = Ax" /
int main ()
{
vector x (2);
x(0) = 1; x(1) = 2;

matrix A(2,2);
A(0,0) = 0; A(0,1) = 1;
A(1,0) = 2; A(1,1) = 3;

vector y = prod(A, x);

std::cout

随机数产生 – Boost.Random
Boost 也提供独立分布的模拟随机与 PRNG 独立性的机率分布,而这些能夠具体的建立产生器。

  • 此范例表示如何使用 Mersenne Twister 演算法来产生随机

#include
#include

using namespace boost;

double SampleNormal (double mean, double sigma)
{
// 建立一个 Mersenne twister 随机数产生器
// 使用 Unix 时间设定 seed
static mt19937 rng(static_cast (std::time(0)));

// 选择高斯机率分布
normal_distribution norm_dist(mean, sigma);

// 使用 function 的形式,生成随机数据产生器
variate_generator > normal_sampler(rng, norm_dist);

// 传回样本分布结果
return normal_sampler();
}

更详细的说明请参阅 [http://boost.org/libs/random/ Boost 随机数库]。

多執行緒 – Boost.Thread
範例碼演示建立執行緒:

#include
#include

using namespace std;

void hello_world()
{
cout

引用
外部連結

评论 (0)

  • 还没有评论,来抢沙发吧。