#include "pch.h"
#include
using namespace std;
template
T f(T *a, T *b, int n)
{
T s = (T)0;
for (int i = 0; i < n; i++)
s += a[i] * b[i];
return s;
}
void main()
{
double c[5] = { 1.1,2.2,3.3,4.4,5.5 }, d[5] = { 10.0,100.0,1000.0 };
cout << f(c, d, 5) << endl;
}
因为传入的是数组类型,数组到了函数里就编程指针了,去掉星号,就不能 a[i] 这样带下标访问了。你试试就知道。
T f(T *a, T *b, int n)
返回类型是T
因为你的数组是double,因此返回值也是double类型(而不是你说的整型)
因为要求传入指针或数据进行位置访问。