C++ 函数模板

2018-03-24 17:52 更新

学习C++ - C++函数模板

函数模板是一个通用的函数描述。

它定义了一个泛型类型的函数。

稍后可以替换特定类型,例如int或double。

通过将类型作为参数传递给模板,编译器将生成一个函数。

因为类型由参数表示,所以参考模板特征作为参数化类型。

函数模板使您能够根据某种任意类型定义函数。

例如,您可以设置如下的交换模板:

template <typename AnyType> 
void Swap(AnyType &a, AnyType &b) 
{ 
     AnyType temp; 
     temp = a; 
     a = b; 
     b = temp; 
} 

第一行设置一个模板,你正在命名AnyType任意类型。

关键字模板和类型名称是强制性的,除了您可以使用关键字类而不是类型名称。

类型名称AnyType是您的选择,只要遵循通常的C ++命名规则即可。

许多程序员使用简单的名字,如T.

其余的代码描述了交换AnyType类型的两个值的算法。


例子

以下代码使用T代替AnyType作为类型参数。


#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; 
}

上面的代码生成以下结果。


例2


  #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; 
  }

上面的代码生成以下结果。

重载模板

当您需要对各种类型应用相同算法的功能时,您可以使用模板。

您可以超载模板定义,就像重载常规函数定义一样。

重载模板需要不同的功能签名。


#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;
}

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号