#include
#include
template
void Sum(const T *t1, const T *t2, T *sum, int t1len, int t2len)
{
int i = 0;
int s = t1len
{
sum[i] = t1[i] + t2[i];
i++;
}
if(t1len > t2len)
{
while(i < t1len)
{
sum[i] = t1[i];
i++;
}
}
else
{
while(i < t2len)
{
sum[i] = t2[i];
i++;
}
}
}
int main()
{
int index = 0;
//int
int a[2] = {1,2};
int b[5] = {1,2,3,4,5};
int c[5] = {0};
Sum(a, b, c, 2, 5);
for(index = 0; index < 5; index++)
{
printf("sum[%d] =%d\n",index, c[index]);
}
//float
float fa[2] = {1.0,2.0};
float fb[5] = {1.0,2.0,3.0,4.0,5.0};
float fc[5] = {0.0};
Sum(fa, fb, fc, 2, 5);
for(index = 0; index < 5; index++)
{
printf("sum[%d] =%f\n",index, fc[index]);
}
return 0;
}
//写了一个,经测试没有问题。
#include
using namespace std;
template
elemType sum(elemType* first, elemType* last)
{
elemType sum = 0;
for (; first != last; ++first)
{
sum += *first;
}
return sum;
}
int main()
{
int integerNum[] = {1,2,3,4,5};
int integerSum = sum(integerNum, integerNum + 5);
cout << "integersum is " << integerSum << endl;
double floatNum[] = {1.1,2.3,4.5};
double floatSum = sum(floatNum, floatNum + 3);
cout << "floatsum is " << floatSum << endl;
return 0;
}