|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请问,为什么这个函数运行结果是这个啊?#include<iostream>
using namespace std;
template <typename T>
T average(T*array, int n = 0)
{
T sum = 0;
T aver = 0;
for (int i = 0; i <= n; i++)
{
sum += array[i];
}
aver = sum / n;
return aver;
};
int main()
{
int iarray[] = { 1,2,3,4,5,6,7,8,9 };
double darray[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9 };
int int_iarray = average(iarray, 9);
double double_darray = average(darray, 9);
cout << int_iarray<< endl;
cout << double_darray<< endl;
return 0;
}
改成这样试试: #include<iostream>
using namespace std;
template <typename T>
T average(T*array, int n = 0)
{
T sum = 0;
T aver = 0;
for (int i = 0; i <= n; i++)
{
sum += array[i];
}
aver = sum / n;
return aver;
};
int main()
{
int iarray[] = { 1,2,3,4,5,6,7,8,9 };
double darray[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9 };
int int_iarray = average<int>(iarray, 9);
double double_darray = average<double>(darray, 9);
cout << int_iarray<< endl;
cout << double_darray<< endl;
return 0;
}
|
-
|