模板的一般形式:
template <class Ttype> return_type function_name(parameter_list)
测试代码:
#include<iostream>
using namespace std;
template <class X> void swapped(X &a, X &b)
{
X temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int i=1,j=2;
float x=1.01,y=2.01;
cout<<"original i and j is "<<i<<' '<<j<<endl;
cout<<"original x and y is "<<x<<' '<<y<<endl;
swapped(i,j);
swapped(x,y);
cout<<"swapped i and j is "<<i<<' '<<j<<endl;
cout<<"swapped x and y is "<<x<<' '<<y<<endl;
return 0;
}
测试结果:
original i and j is 1 2
original x and y is 1.01 2.01
swapped i and j is 2 1
swapped x and y is 2.01 1.01
请按任意键继续. . .