---
title: "C++【函数模板】"
author: "y_x"
date: "2014-07-08T12:50:42Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2014-07-08T16:35:20Z"
id: 2089461
url: "https://y-x.iteye.com/blog/2089461"
---

```
  9 /**
 10  * @file test_template.cpp
 11  * @author yixiu01(com@baidu.com)
 12  * @date 2014/07/08 11:33:51
 13  * @brief-模板学习
 14  *--
 15  **/
 16 #include<iostream>
 17 using namespace std ;
 18 //定义模板的一般形式
 19 //函数模板只适用于函数的参数个数相同而类型不同， 且函数体相同的情况
 20 template<typename T>
 21 T max(T a, T b, T c)
 22 {   
 23     if(b>a) 
 24         a = b;
 25     if(c>a) 
 26         a = c;
 27     return a;
 28 }
 29 
 30 int main()
 31 {   
 32     int i1=1, i2=4, i3=5, i_max;
 33     i_max = max(i1,i2, i3);
 34     cout<<"i_max="<<i_max<<endl;
 35     
 36     long g1=778, g2=-913, g3=7777,g_max;
 37     g_max = max(g1, g2, g3);
 38     cout<<"g_max="<<g_max<<endl;
 39 }
```