www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第5章/s5_1/smain5_1.cpp
//Download by http://www.NewXing.com //smain5_1.cpp //该程序实现在一个数组中求最大值 #include <iostream> using namespace std; //定义函数模板1 template <class Type> Type GetMax(Type a[], int iCnt, char *lpszArrayName) { int i; //定义数组下标变量i Type tMaxValue = a[0]; //定义Type类型的变量 for (i=1; i<iCnt; i++) //在循环中寻找数组中最大的值 { if (tMaxValue < a[i]) { tMaxValue = a[i]; //保存最大值到tMaxValue } } cout << "使用函数模板1," << lpszArrayName << "的最大值,maxValue = "; return tMaxValue; } //主函数 void main() { int a[] = {1, 3, 5, 7, 9, 6, 4, 8, 2, 10}; double b[] = {3.2, -6.4, 6.0, 9.9, 8.6, 2.1}; char c[] = {'A', 'C', '1', 'a', 'c'}; cout << " " << GetMax(a, 10, "数组a") << endl; //使用函数模板1 cout << " " << GetMax(b, 5, "数组b") << endl; //使用函数模板1 cout << " " << GetMax(c, 5, "数组c") << endl; //使用函数模板1 cin.get(); }