[原]此程序专用来说明C++模板的用法
#include
using namespace std;
//此程序专用来说明模版的使用
template
void swap1(T& a,T& b){
T
temp=a;
a=b;
b=temp;
}
int main(){
double
a1=3.2;
double
a2=3.3;
cout<<"a1="<<a1<<endl;
cout<<"a2="<<a2<<endl;
swap1(a1,a2);
cout<<"a1="<<a1<<endl;
cout<<"a2="<<a2<<endl;
int
b1=1,b2=2;
cout<<"b1="<<b1<<endl;
cout<<"b2="<<b2<<endl;
swap1(b1,b2);
cout<<"b1="<<b1<<endl;
cout<<"b2="<<b2<<endl;
char*
c1="Hello!";
char*
c2="Hi!";
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
swap1(c1,c2);
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
}
using namespace std;
//此程序专用来说明模版的使用
template
void swap1(T& a,T& b){
}
int main(){
}
=================================================================================================
#include
using namespace std;
template
T max1(T a,T b)
{
return a
}
int main()
{
int a=5;
float b=3.5;
cout<<max1(a,b)<<endl;
}
作者:gcy77 发表于2014-3-12 15:16:30 原文链接
阅读:56 评论:0 查看评论