#include <iostream>
//模版类的声明
template<typename T>
void Swap(T& a,T& b);
int main()
{
int i = 10;
int j = 20;
std::cout<<"i=" << i << "," << "j=" <<j;
Swap(i,j);//生成 void Swap(int &,int&);
std::cout<<"i=" << i << "," << "j=" <<j;
double x = 11.5;
double y = 19.5;
std::cout<<"x=" << x << "," << "y=" <<y;
Swap(x,y);//编译器生成 void Swap(double &,double&);
std::cout<<"x=" << x << "," << "y=" <<y;
return 0;
}
//模版类的定义
template<typename T>
void Swap(T& a,T& b)
{
T temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
//模版类的声明
template<typename T>
void Swap(T& a,T& b);
const int iCount = 5;
template<typename T>
void Swap(T* a,T*b,int n);
int main()
{
int i = 10;
int j = 20;
std::cout<<"i=" << i << "," << "j=" <<j;
Swap(i,j);//生成 void Swap(int &,int&)
std::cout<<"i=" << i << "," << "j=" <<j;
double x = 11.5;
double y = 19.5;
std::cout<<"x=" << x << "," << "y=" <<y;
Swap(x,y);//编译器生成 void Swap(double &,double&);
std::cout<<"x=" << x << "," << "y=" <<y;
int d[iCount] = {0,1,2,3,4};
int e[iCount] = {5,6,7,8,9};
Swap(d,e,iCount);//匹配新的模版,进行数组的交换
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)
{
for (int i=0;i<iCount;++i)
{
T temp;
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
void Swap(job &,job&);//非模版函数 template <typename T> void Swap(T&,T&);//模版函数 template <> void Swap<job>(job&,job&);//显示具体化函数,其中Swap后的job参数可去掉,则函数签名为template <> void Swap(job&,job&);
template void Swap<int>(int,int);//显示实例化
template <> void Swap<int>(int,int); template <> void Swap(int,int);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有