Boost C++ Libraries
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(页面存档备份,存于互联网档案馆)。 內容
范例现有的 Boost 包含大约150种不同的函数库,以下面几项做范例: 线性代数 – uBLASBoost 包含了 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.RandomBoost 也提供独立分布的模拟随机与 PRNG 独立性的机率分布,而这些能夠具体的建立产生器。
#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;
}
引用
外部連結維基教科書中的相關電子教程:Libraries/Boost |
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve