C++ Technical Report 1

出自Local Chinese Wikipedia
(重新導向自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> 標頭檔中增加了 - crefrefreference_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::bind1ststd::bind2nd的通用版
  • 將參數綁定給仿函數,並且允許函數的結合。
函數返回型別
  • result_of
  • 採納自Boost
  • 決定函數呼叫的返回型別
成員函數
  • mem_fn
  • 採納自Boost Mem Fn library[5]
  • 標準std::mem_funstd::mem_fun_ref的加強版
  • 允許成員函數指標能夠像仿函數一樣

元程式設計和型別特性(Type Traits)[編輯]

  • 新的<type_traits>標頭檔 - is_podhas_virtual_destructorremove_extent
  • 採納自Boost Type Traits library[6]
  • 允許類編查詢以及類別間的轉換,可促進元程式設計

數值工具[編輯]

亂數產生器[編輯]

  • 新的<random>標頭檔 - variate_generatormersenne_twisterpoisson_distribution
  • 採納自Boost Random Number Library[7]

數學函數[編輯]

  • 新的<cmath>/<math.h>標頭檔 - betalegendre
  • 23種數學函數
函數名 函數原型 數學表達式
連帶拉蓋爾多項式 double assoc_laguerre( unsigned n, unsigned m, double x ) ; <math>{L_n}^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n+m}(x), \text{ for } x \ge 0</math>
連帶勒壤得多項式 double assoc_legendre( unsigned l, unsigned m, double x ) ; <math>{P_l}^m(x) = (1-x^2)^{m/2} \frac{d^m}{dx^m} P_l(x), \text{ for } x \ge 0</math>
Beta 函數 double beta( double x, double y ) ; <math>\Beta(x,y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)}</math>
第一類完全橢圓積分 double comp_ellint_1( double k ) ; <math>K(k) = F\left(k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1 - k^2 \sin^2 \theta}}</math>
第二類完全橢圓積分 double comp_ellint_2( double k ) ; <math>E\left(k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \sqrt{1 - k^2 \sin^2 \theta}\; d\theta</math>
第三類完全橢圓積分 double comp_ellint_3( double k , double nu ) ; <math>\Pi\left(\nu, k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \frac{d\theta}{(1 - \nu \sin^2 \theta)\sqrt{1 - k^2 \sin^2 \theta}}</math>
合流超幾何函數 double conf_hyperg( double a, double c, double x ) ; <math>F(a, c, x) = \frac{\Gamma(c)}{\Gamma(a)} \sum_{n = 0}^\infty \frac{\Gamma(a + n) x^n}{\Gamma(c + n) n!}</math>
第一類變形貝塞爾函數 double cyl_bessel_i( double nu, double x ) ; <math>I_\nu(x) = i^{-\nu} J_\nu(ix) = \sum_{k = 0}^\infty \frac{(x/2)^{\nu + 2k}}{k! \; \Gamma(\nu + k + 1)}, \text{ for } x \ge 0</math>
第二類變形貝塞爾函數 double cyl_bessel_j( double nu, double x ) ; <math>J_\nu(x) = \sum_{k = 0}^\infty \frac{(-1)^k \; (x/2)^{\nu + 2k}}{k! \; \Gamma(\nu + k + 1)}, \text{ for } x \ge 0</math>
第三類變形貝塞爾函數 double cyl_bessel_k( double nu, double x ) ; <math>\begin{align}

K_\nu(x) & = \textstyle\frac{\pi}{2} i^{\nu+1} \big(J_\nu(ix) + i N_\nu(ix)\big) \\

        & = \begin{cases}
                \displaystyle \frac{I_{-\nu}(x) - I_\nu(x)}{\sin \nu\pi}, & \text{for } x \ge 0 \text{ and } \nu \notin \mathbb{Z} \\[10pt]
                \displaystyle \frac{\pi}{2} \lim_{\mu \to \nu} \frac{I_{-\mu}(x) - I_\mu(x)}{\sin \mu\pi}, & \text{for } x < 0 \text{ and } \nu \in \mathbb{Z} \\
            \end{cases}

\end{align}</math>

柱諾依曼函數

第二類柱貝塞爾函數

double cyl_neumann( double nu, double x ) ; <math>

N_\nu(x) = \begin{cases}

                \displaystyle \frac{J_\nu(x)\cos \nu\pi - J_{-\nu}(x)}{\sin \nu\pi}, & \text{for } x \ge 0 \text{ and } \nu \notin \mathbb{Z} \\[10pt]
                \displaystyle \lim_{\mu \to \nu} \frac{J_\mu(x)\cos \mu\pi - J_{-\mu}(x)}{\sin \mu\pi}, & \text{for } x < 0 \text{ and } \nu \in \mathbb{Z} \\
            \end{cases}

</math>

第一類不完全橢圓積分 double ellint_1( double k, double phi ) ; k\right| \le 1</math>
第二類不完全橢圓積分 double ellint_2( double k, double phi ) ; k\right| \le 1</math>
第三類不完全橢圓積分 double ellint_3( double k, double nu, double phi ) ; k\right| \le 1</math>
指數積分 double expint( double x ) ; <math> \mbox{E}i(x)=-\int_{-x}^{\infty} \frac{e^{-t}}{t}\, dt</math>
埃爾米特多項式 double hermite( unsigned n, double x ) ; <math>H_n(x)=(-1)^n e^{x^2}\frac{d^n}{dx^n}e^{-x^2}\,\!</math>
超幾何級數 double hyperg( double a, double b, double c, double x ) ; <math>F(a,b,c,x)=\frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}\sum_{n = 0}^\infty\frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}\frac{x^n}{n!}</math>
拉蓋爾多項式 double laguerre( unsigned n, double x ) ; <math>L_n(x)=\frac{e^x}{n!}\frac{d^n}{dx^n}\left(x^n e^{-x}\right), \text{ for } x \ge 0</math>
勒壤得多項式 double legendre( unsigned l, double x ) ; x\right| \le 1 </math>
黎曼zeta函數 double riemann_zeta( double x ) ; <math>

\Zeta(x) =

         \begin{cases}
                \displaystyle \sum_{k = 1}^\infty k^{-x}, & \text{for } x > 1 \\[10pt]
                \displaystyle 2^x\pi^{x-1}\sin\left(\frac{x\pi}{2}\right)\Gamma(1-x)\zeta(1-x), & \text{for } x < 1 \\
            \end{cases}

</math>

第一類球貝塞爾函數 double sph_bessel( unsigned n, double x ) ; <math>j_n(x) = \sqrt{\frac{\pi}{2x}} J_{n+1/2}(x), \text{ for } x \ge 0</math>
球諧函數 double sph_legendre( unsigned l, unsigned m, double theta ) ; m| \leq l</math>
球諾依曼函數

第二類球貝塞爾函數

double sph_neumann( unsigned n, double x ) ; <math>n_n(x) = \left(\frac{\pi}{2x}\right)^{\frac{1}{2}}N_{n+\frac{1}{2}}(x), \text{ for } x \ge 0</math>

容器[編輯]

多元組型別(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>標頭檔 - regexregex_matchregex_searchregex_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). 

其他[編輯]

外部連結[編輯]