函数模版是通用的函数描述,有时候也被称为参数化类型。
建立模版如下:
template <class Any>
void Swap (Any&a,Any&b){
Any temp;
temp = a;
a=b;
b =temp;
}
#include <iostream>
template <typename Any>
void Swap(Any& a,Any& b);
struct job
{
char name[40];
double salary;
int floor;
};
template <> void Swap<job>(job& job1,job& job2);
void Show(job &j);
int main()
{
using namespace std;
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
int i= 10, j = 20;
cout<<"i,j"<<i<<","<<j<<endl;
cout<<"Using compiler-generated int swapper: "<<endl;
Swap(i,j);
cout<<"Now i,j = "<<i<<","<<j<<endl;
job sue = {"Susan",74000.60,7};
job sidney = {"Sidney",78060.72,9};
cout<<"Before job swapping:"<<endl;
Show(sue);
Show(sidney);
Swap(sue,sidney);
cout<<"After job swapping:"<<endl;
Show(sue);
Show(sidney);
system("pause");
return 0;
}
template <typename Any>
void Swap(Any& a,Any& b)
{
Any temp =a;
a = b;
b = temp;
}
template<> void Swap<job>(job& job1,job& job2)
{
double t1;
int t2;
t1 = job1.salary;
job1.salary = job2.salary;
job2.salary = t1;
t2 = job1.floor;
job1.floor = job2.floor;
job2.floor = t2;
}
void Show(job &j)
{
using namespace std;
cout<<j.name<<":$"<<j.salary<<" on floor "<<j.floor<<endl;
}#include <iostream>
template <class Any>//定义一个模版(用class)
void Swap(Any& a,Any& b);//定义一个模版函数
template <typename Any>//定义一个模版(用typename)
void Swap(Any& a,Any& b,int c);
void show(int a[]);
const int Lim = 8;
int main()
{
using namespace std;
int i =0 ,j = 20;
cout<<"i,j= "<<i<<","<<j<<endl;
cout<<"using compiler-generated int Swapper"<<endl;
Swap(i,j);
cout<<"Now i,j = "<<i<<","<<j<<endl;
int d1[Lim] = {0,7,0,4,1,7,7,6};
int d2[Lim] = {0,6,2,0,1,9,6,9};
cout<<"Original arrays:"<<endl;
show(d1);
show(d2);
system("pause");
return 0;
}
template <typename Any>
void Swap(Any& a,Any& b)
{
Any temp;
temp = a;
a= b;
b=temp;
}
template <typename Any>
void Swap(Any& a,Any& b,int c)
{
for (int i =0 ;i<c;i++)
{
Any temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void show(int a[])
{
using namespace std;
cout<<a[0]<<a[1]<<"/";
cout<<a[2]<<a[3]<<"/";
for (int i = 4;i<Lim;i++)
{
cout<<a[i];
}
cout<<endl;
}
#include <iostream>
template <typename Any>
void Swap(Any& a,Any& b);
struct job
{
char name[40];
double salary;
int floor;
};
template <> void Swap<job>(job& job1,job& job2);//显示具体化声明
void Show(job &j);
int main()
{
using namespace std;
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
int i= 10, j = 20;
cout<<"i,j"<<i<<","<<j<<endl;
cout<<"Using compiler-generated int swapper: "<<endl;
Swap(i,j);
cout<<"Now i,j = "<<i<<","<<j<<endl;
job sue = {"Susan",74000.60,7};
job sidney = {"Sidney",78060.72,9};
cout<<"Before job swapping:"<<endl;
Show(sue);
Show(sidney);
Swap(sue,sidney);
cout<<"After job swapping:"<<endl;
Show(sue);
Show(sidney);
system("pause");
return 0;
}
template <typename Any>
void Swap(Any& a,Any& b)
{
Any temp =a;
a = b;
b = temp;
}
template<> void Swap<job>(job& job1,job& job2)//显示具体化定义(不需要再声明template <typename Any>)
{
double t1;
int t2;
t1 = job1.salary;
job1.salary = job2.salary;
job2.salary = t1;
t2 = job1.floor;
job1.floor = job2.floor;
job2.floor = t2;
}
void Show(job &j)
{
using namespace std;
cout<<j.name<<":$"<<j.salary<<" on floor "<<j.floor<<endl;
}
显示具体化两种等价声明:
template<>void Swap<int>(int &,int&); ==template<> void Swap(int &,int&);
原文:http://blog.csdn.net/ztk881012/article/details/17650465