template (a>b)?a:b cin get()

//  STL  C/C++  template<class T>  (a>b)?a:b  cin.get();

#include<iostream.h>
#include<string.h>

//定义函数模板

template<class T>  //template 是关键字,T 表示一种待实例化的类型


//template<typename T>  也是对的

T max(T a, T b)//函数模板,函数名为 max,此函数有2个T类型的参数,返回类型为T

{
	return (a>b)?a:b;  
}

//在此例实例化的时候,T可以是多种类型的,int,char,string
void main()
{
	int x=2,y=6;
	double x1=9.123,y1=12.6543;
	cout<<"把T实例化为int:"<<max(x,y)<<endl;//实例化函数模板,把T实例化为int

	cout<<"把T实例化为double:"<<max(x1,y1)<<endl; 
	//实例化函数模板,把T实例化为double

	cin.get();//这一行代码用来在dos下查看结果

}


★参考资料★ 
http://www.ok2002.com/cc/html/4y.83t_8ej.-5uq29pa-vdcn03rraydx7pvqizgtmf9ls7oowfwk5bx4bcg2khl_.html

转自:http://www.ok2002.com/cc/html/4y.83t_8ej.-5uq29pa-vdcn03rraydx7pvqizgtmf9ls7oowfwk5bx4bcg2khl_.html

posted @ 2013-04-07 19:04  stma  阅读(244)  评论(0)    收藏  举报