# 实例一

#include <string>
#include <iostream>

using namespace std;

template<typename T> void print(const T& var)
{
cout << var << endl;
}
int main()
{
string str("Hello World");
const int num=12345;
print(str);
print(num);
return 0;
}

# 实例二

#include <iostream.h>
// 定义函数模板，找出三个值中最小的值，与数据类型无关
template <class T>
T min(T ii, T jj, T kk)
{
T temp;
if((ii<jj)&&(ii<kk)){     temp=ii; }
else if((jj<ii)&&(jj<kk)){ temp=jj; }
else{    temp=kk;}
return temp;
}
int main()
{
cout<<min("anderson","Washington","Smith")<<endl;
return 0;
}

# 实例三

#include <iostream>
#include <string>

using namespace std;

template<class T> void swap(T *x, T *y){//函数的模板，只要能使用等号赋值类型都可以用这个模板互换
T temp = *x;
*x = *y;
*y = temp;
}

int main()
{
int i = 9, j = 5;
float k = 9.2, l = 5.6;
swap(&i, &j);//交换整形
swap(&k, &l);//交换浮点型
cout<<"i = "<<i<<" , j = "<<j<<endl;
cout<<"k = "<<k<<" , l = "<<l<<endl;
return 0;
}

# 实例四

#include<iostream>

using namespace std;

{
int c;
c=a+b;
return c;
}

{
double c;
c=a+b;
return c;
}

int main()
{
return 0;
}

#include<iostream>

using namespace std;

{
int c;
c=a+b;
return c;
}

{
double c;
c=a+b;
return c;
}

int main()
{
return 0;
}

#include<iostream>

using namespace std;

template<typename T>
{
T c;
c=a+b;
return c;
}

int main()
{
return 0;
}

09-19 370

01-08 3813

02-26 2802

05-07 1.1万

02-07 208

04-13 243

07-11 549

12-08 117

04-03 848