跳转到内容

C++ Technical Report 1

本页使用了标题或全文手工转换
维基百科,自由的百科全书
(重定向自TR1)

C++ Technical Report 1(TR1)是ISO/IEC TR 19768, C++ Library Extensions(函式庫擴充)的一般名稱。TR1是一份文件,內容提出了對C++標準函式庫的追加項目。這些追加項目包括了正则表达式、智能指针、哈希表、随机数生成器等。TR1自己並非標準,它是一份草稿文件。然而它所提出的項目大多数已成為的C++11及之后版本的官方標準的一部分。這份文件的目標在於「為擴充的C++標準函式庫建立更為廣泛的現成實作品」。

概要

[编辑]

編譯器並不需要保證包含TR1的組件,因為TR1并非官方标准的一部分。順帶一提,Boost提供了TR1大部分的實作,數個編譯器/函式庫開發商也已提供了各自的實作版本。

TR1並不代表下一屆標準的全部;舉例而言,下一屆的標準C++11包含了线程的支援。

新的組件被放置在std::tr1的命名空間(namespace)裡,以和現在的標準函式庫做區別。

TR1的內容

[编辑]

TR1包含以下組件:

一般用途

[编辑]
引用包装器(Reference Wrapper)
  • 來自Boost.Ref [1]
  • 在<functional> 头文件中增加了 - cref、ref、reference_wrapper
  • 可以对算法(algorithms)或仿函数(function objects)传递引用(references),而不是传递副本。

一個wrapper reference是由模板类reference_wrapper產生的實體(instance)獲得。wrapper reference近似於C++語言中的引用。

使用ref以獲得任何实例的wrapper reference(對常数引用const &使用cref)。

wrapper reference對模板函数(template function)尤其有用,當模板参数推導不出引用的時候(範例如下:)

void f( int &r ) { r++; }

template< class Funct, class Arg >
void g( Funct f, Arg t )
{
  f(t);
}

int main()
{
  int i = 0;
  g( f, i );          // 'g< void(int &r), int >' 被实例化
  cout << i << endl;  // 輸出:0

  g( f, ref(i) );     // 'g< void(int &r), reference_wrapper<int> >' 被实例化
  cout << i << endl;  // 輸出:1
}
智能指针(Smart Pointers)

仿函数

[编辑]

以下四個模組被加進<functional>標頭檔之中:

多形態的函式包裝器(Polymorphic Function Wrapper)
  • function
  • 基於Boost.Function[3]
  • 儲存任何使用特定函式簽名的"可呼叫物"(函数指针、成員函式指针、仿函数),不需要可呼叫物確切的型別。
仿函数綁定器(Function Object Binders)
  • bind
  • 採納自Boost Bind library[4]
  • 標準std::bind1st和std::bind2nd的通用版
  • 將參數綁定給仿函数,並且允許函式的結合。
函式返回型別
  • result_of
  • 採納自Boost
  • 決定函式呼叫的返回型別
成員函式
  • mem_fn
  • 採納自Boost Mem Fn library[5]
  • 標準std::mem_fun和std::mem_fun_ref的加強版
  • 允許成員函式指针能夠像仿函数一樣

元編程和型別特性(Type Traits)

[编辑]
  • 新的<type_traits>头文件 - is_pod、has_virtual_destructor、remove_extent等
  • 採納自Boost Type Traits library[6]
  • 允許类编查询以及类别間的轉換,可促進元編程

數值工具

[编辑]

随机数產生器

[编辑]
  • 新的<random>头文件 - variate_generator、mersenne_twister、poisson_distribution等
  • 採納自Boost Random Number Library[7]

數學函式

[编辑]
  • 新的<cmath>/<math.h>头文件 - beta、legendre等
  • 23種數學函式
函数名 函数原型 数学表达式
连带拉盖尔多项式 double assoc_laguerre( unsigned n, unsigned m, double x ) ; Lnm(x)=(−1)mdmdxmLn+m(x), for x≥0
连带勒让德多项式 double assoc_legendre( unsigned l, unsigned m, double x ) ; Plm(x)=(1−x2)m/2dmdxmPl(x), for x≥0
Beta 函数 double beta( double x, double y ) ; B(x,y)=Γ(x)Γ(y)Γ(x+y)
第一类完全椭圆积分 double comp_ellint_1( double k ) ; K(k)=F(k,π2)=∫0π2dθ1−k2sin2θ
第二类完全椭圆积分 double comp_ellint_2( double k ) ; E(k,π2)=∫0π21−k2sin2θdθ
第三类完全椭圆积分 double comp_ellint_3( double k , double nu ) ; Π(ν,k,π2)=∫0π2dθ(1−νsin2θ)1−k2sin2θ
合流超几何函数 double conf_hyperg( double a, double c, double x ) ; F(a,c,x)=Γ(c)Γ(a)∑n=0∞Γ(a+n)xnΓ(c+n)n!
第一类变形贝塞尔函数 double cyl_bessel_i( double nu, double x ) ; Iν(x)=i−νJν(ix)=∑k=0∞(x/2)ν+2kk!Γ(ν+k+1), for x≥0
第二类变形贝塞尔函数 double cyl_bessel_j( double nu, double x ) ; Jν(x)=∑k=0∞(−1)k(x/2)ν+2kk!Γ(ν+k+1), for x≥0
第三类变形贝塞尔函数 double cyl_bessel_k( double nu, double x ) ; Kν(x)=π2iν+1(Jν(ix)+iNν(ix))={I−ν(x)−Iν(x)sin⁡νπ,for x≥0 and ν∉ℤπ2limμ→νI−μ(x)−Iμ(x)sin⁡μπ,for x<0 and ν∈ℤ
柱诺依曼函数

第二类柱贝塞尔函数

double cyl_neumann( double nu, double x ) ; Nν(x)={Jν(x)cos⁡νπ−J−ν(x)sin⁡νπ,for x≥0 and ν∉ℤlimμ→νJμ(x)cos⁡μπ−J−μ(x)sin⁡μπ,for x<0 and ν∈ℤ
第一类不完全椭圆积分 double ellint_1( double k, double phi ) ; F(k,ϕ)=∫0ϕdθ1−k2sin2θ, for |k|≤1
第二类不完全椭圆积分 double ellint_2( double k, double phi ) ; E(k,ϕ)=∫0ϕ1−k2sin2θdθ, for |k|≤1
第三类不完全椭圆积分 double ellint_3( double k, double nu, double phi ) ; Π(k,ν,ϕ)=∫0ϕdθ(1−νsin2θ)1−k2sin2θ, for |k|≤1
指数积分 double expint( double x ) ; Ei(x)=−∫−x∞e−ttdt
埃爾米特多項式 double hermite( unsigned n, double x ) ; Hn(x)=(−1)nex2dndxne−x2
超几何级数 double hyperg( double a, double b, double c, double x ) ; F(a,b,c,x)=Γ(c)Γ(a)Γ(b)∑n=0∞Γ(a+n)Γ(b+n)Γ(c+n)xnn!
拉盖尔多项式 double laguerre( unsigned n, double x ) ; Ln(x)=exn!dndxn(xne−x), for x≥0
勒让德多项式 double legendre( unsigned l, double x ) ; Pl(x)=12ll!dldxl(x2−1)l, for |x|≤1
黎曼zeta函数 double riemann_zeta( double x ) ; Z(x)={∑k=1∞k−x,for x>12xπx−1sin⁡(xπ2)Γ(1−x)ζ(1−x),for x<1
第一类球贝塞尔函数 double sph_bessel( unsigned n, double x ) ; jn(x)=π2xJn+1/2(x), for x≥0
球谐函数 double sph_legendre( unsigned l, unsigned m, double theta ) ; Ylm(θ,0) where Ylm(θ,ϕ)=(−1)m[(2l+1)4π(l−m)!(l+m)!]12Plm(cos⁡θ)eimϕ, for |m|≤l
球诺依曼函数

第二类球贝塞尔函数

double sph_neumann( unsigned n, double x ) ; nn(x)=(π2x)12Nn+12(x), for x≥0

容器

[编辑]

多元組型別(Tuple Types)

[编辑]
  • 新<tuple>標頭檔 - tuple
  • 採納自Boost Tuple library[8]
  • 標準std::pair的擴充
  • 固定尺寸的元素集合,元素可以是不同的类型

定量陣列(Fixed Size Array)

[编辑]
  • 新<array>標頭檔 - array
  • 來自Boost Array library[9]
  • 与動態陣列型別,像是標準的std::vector相反,是静态的矩阵,但是能够享受类似于begin()等与std::vector相似的接口。

哈希表(Hash Tables)

[编辑]
  • 新<unordered_set>、<unordered_map>標頭檔
  • 完全是新的實作,不衍生自既有之程式庫。與既有之程式庫API並不完全相容
  • 就如同所有的哈希表提供常數時間的元素查找,但最壞情況查找時間與容器的大小呈線性關係。

正規表示式

[编辑]
  • 新<regex>標頭檔 - regex、regex_match、 regex_search、regex_replace等
  • 来自Boost RegEx library[10]
  • pattern matching library

C的兼容性

[编辑]

C++被設計成與C語言兼容,但由於不同的標準,C++並不是C的嚴格超集合。TR1試圖調和這些差異,透過對各種標頭檔,如<complex>、<locale>、<cmath>等進行擴充。 這些改變幫助C++能夠與C99版本的C標準更為一致(並非所有C99都包含於TR1)。

關聯項目

[编辑]

参考文献

[编辑]

腳註

[编辑]
  1. ^ ref - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  2. ^ Boost.SmartPtr: The Smart Pointer Library - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  3. ^ Chapter 16. Boost.Function - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  4. ^ Chapter 1. Boost.Bind - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  5. ^ Chapter 1. Boost.Member Function - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  6. ^ Chapter 1. Boost.TypeTraits - 1.37.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-15). 
  7. ^ [1](页面存档备份,存于互联网档案馆)
  8. ^ The Boost Tuple Library – Boost 1.48.0. [2006-05-27]. (原始内容存档于2006-05-26). 
  9. ^ Chapter 5. Boost.Array - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-01). 
  10. ^ Boost.Regex - 1.36.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-11). 

其他

[编辑]

外部連結

[编辑]