描述一下该程序执行的过程,为什么T f(T *a,T *b,int n)里要有*?还有结果为什么是一个整形?

#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类型(而不是你说的整型)

如果问题解决,请点我回答左上角的采纳,谢谢

因为要求传入指针或数据进行位置访问。