函數(shù)模板是一個(gè)通用的函數(shù)描述。
它定義了一個(gè)泛型類型的函數(shù)。
稍后可以替換特定類型,例如int或double。
通過將類型作為參數(shù)傳遞給模板,編譯器將生成一個(gè)函數(shù)。
因?yàn)轭愋陀蓞?shù)表示,所以參考模板特征作為參數(shù)化類型。
函數(shù)模板使您能夠根據(jù)某種任意類型定義函數(shù)。
例如,您可以設(shè)置如下的交換模板:
template <typename AnyType> void Swap(AnyType &a, AnyType &b) { AnyType temp; temp = a; a = b; b = temp; }
第一行設(shè)置一個(gè)模板,你正在命名AnyType任意類型。
關(guān)鍵字模板和類型名稱是強(qiáng)制性的,除了您可以使用關(guān)鍵字類而不是類型名稱。
類型名稱AnyType是您的選擇,只要遵循通常的C ++命名規(guī)則即可。
許多程序員使用簡單的名字,如T.
其余的代碼描述了交換AnyType類型的兩個(gè)值的算法。
以下代碼使用T代替AnyType作為類型參數(shù)。
#include <iostream>
using namespace std;
// function template prototype
template <typename T> // or class T
void Swap(T &a, T &b);
int main(){
int i = 10;
int j = 20;
cout << "i, j = " << i << ", " << j << ".\n";
Swap(i,j); // generates void Swap(int &, int &)
cout << "Now i, j = " << i << ", " << j << ".\n";
double x = 2.5;
double y = 8.7;
cout << "x, y = " << x << ", " << y << ".\n";
Swap(x,y); // generates void Swap(double &, double &)
cout << "Now x, y = " << x << ", " << y << ".\n";
return 0;
}
// function template definition
template <typename T> // or class T
void Swap(T &a, T &b)
{
T temp; // temp a variable of type T
temp = a;
a = b;
b = temp;
}
上面的代碼生成以下結(jié)果。
#include <iostream>
using namespace std;
// Definition of function template maximum.
template < typename T > // or template< typename T >
T maximum( T value1, T value2, T value3 )
{
T maximumValue = value1; // assume value1 is maximum
// determine whether value2 is greater than maximumValue
if ( value2 > maximumValue )
maximumValue = value2;
// determine whether value3 is greater than maximumValue
if ( value3 > maximumValue )
maximumValue = value3;
return maximumValue ;
} // end function template maximum
int main()
{
// demonstrate maximum with int values
int int1, int2, int3;
cout << "Input three integer values: ";
cin >> int1 >> int2 >> int3;
// invoke int version of maximum
cout << "The maximum integer value is: "
<< maximum( int1, int2, int3 );
// demonstrate maximum with double values
double double1, double2, double3;
cout << "\n\nInput three double values: ";
cin >> double1 >> double2 >> double3;
// invoke double version of maximum
cout << "The maximum double value is: "
<< maximum( double1, double2, double3 );
// demonstrate maximum with char values
char char1, char2, char3;
cout << "\n\nInput three characters: ";
cin >> char1 >> char2 >> char3;
// invoke char version of maximum
cout << "The maximum character value is: "
<< maximum( char1, char2, char3 ) << endl;
}
上面的代碼生成以下結(jié)果。
當(dāng)您需要對各種類型應(yīng)用相同算法的功能時(shí),您可以使用模板。
您可以超載模板定義,就像重載常規(guī)函數(shù)定義一樣。
重載模板需要不同的功能簽名。
#include <iostream>
using namespace std;
template <typename T> // original template
void Swap(T &a, T &b);
template <typename T> // new template
void Swap(T *a, T *b, int n);
void Show(int a[]);
const int Lim = 8;
int main(){
int i = 10, j = 20;
cout << "i, j = " << i << ", " << j << ".\n";
Swap(i,j); // matches original template
cout << "Now i, j = " << i << ", " << j << ".\n";
int d1[Lim] = {0,7,2,4};
int d2[Lim] = {1,7,2,0};
Show(d1);
Show(d2);
Swap(d1,d2,Lim); // matches new template
Show(d1);
Show(d2);
return 0;
}
template <typename T>
void Swap(T &a, T &b) {
T temp;
temp = a;
a = b;
b = temp;
}
template <typename T>
void Swap(T a[], T b[], int n){
T temp;
for (int i = 0; i < n; i++) {
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void Show(int a[]){
cout << a[0] << a[1] << "/";
cout << a[2] << a[3] << "/";
for (int i = 4; i < Lim; i++)
cout << a[i];
cout << endl;
}
上面的代碼生成以下結(jié)果。
更多建議: