Template function 函数模板用法

#include<iostream>
using namespace std;

const double PI = 3.1415926;

template <class T>
T min(T a[], int n){
	int i;
	T minv = a[0];
	for (i = 1; i < n; i++){
		if (a[i] < minv)
			minv = a[i];
	}
	return minv;
}

template<class T1>
double Circle_Square(T1 x){
	cout << "From template function";
	return x*x*PI;
}

double Circle_Square(long x){
	cout << "From long type function";
	return x*x*PI;
}

int main(){
	int a[] = { 2, 4, 1, 5, 6, 87, 5, 5, 6 };
	double b[] = { 2.33, 4.01, 3.0, 1.011 };
	cout << "min of a[]: " << min(a, 9) << endl;
	cout << "min of b[]: " << min(b, 4) << endl;

	int r1 = 1;  
	double r2 = 2.0;
	long r3 = 3;     //use the Overloaded functions if has, then refer to the template functions
	cout << " r1 " << Circle_Square(r1) << endl;
	cout << " r2 " << Circle_Square(r2) << endl;
	cout << " r3 " << Circle_Square(r3) << endl;
	int i;
	cin >> i;
	return 0;
}

Results:  

转载于:https://www.cnblogs.com/bruceyo/p/3850126.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值