c++模板举例--简单
源程序:
#include <iostream>
using namespace std;
template <class T>
void f(T a[], int n)
{
T t = 0;
for (int i = 0; i < n / 2; i++)
{
t = a[i]; a[i] = a[n - 1 - i]; a[n - 1 - i] = t;
}
}
int main()
{
int a[5] = { 10,21,34,4,50 };
double d[6] = { 11.1,10.1,9.1,8.1,7.1,6.1 };
f(a, 5);
f(d, 6);
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
cout << endl;
for (int i = 0; i < 6; i++)
cout << d[i] << " ";
cout << endl;
system("pause");
return 0;
}
运行结果: