模板函数与重载是密切相关的。
模板函数本身可以用多种方式重载,这需要提供其他函数模板,
指定不同参数的相同函数名。
- #include<iostream>
- using namespace std;
- template <class T>
- void dispArr(T *arr, int n)//函数模板1
- {
- int i;
- for(i=0;i<n;i++)
- cout<<arr[i]<<" ";
- cout<<endl;
- }
- template <class T>
- void dispArr(T *arr, int i, int j)//函数模板2
- {
- int k;
- for(k=i;k<j;k++)
- cout<<arr[k]<<" ";
- cout<<endl;
- }
- int main()
- {
- int a[] = {1,2,3,4,5,6,7,8,9,10};
- double b[]={1.2,9.5,6.8,2.5,3.5,4.8,1.8};
- cout<<"a:";
- dispArr(a,8);
- cout<<"b:";
- dispArr(b, 2, 6);
- return 0;
- }