我只是测试一下T为int型可以使用,希望对你有帮助。
#include
using namespace std;
template
T Sum(T arry[],int n)
{
T sum=0;
for(int i=0;i{
sum+=arry[i];
}
return sum;
}
template
int Search(T arry[],int n,T data)
{
for(int i=0;i{
if(arry[i]==data)
return i;
}
return -1;
}
int main()
{
int n,m,data;
cout<<"please input n: ";
cin>>n;
cout<<"please input n numbers: ";
int *arry=new int[n];
for(int i=0;icin>>arry[i];
arry[0]=Sum(arry,n);
cout<<"The sum of array is :"<cout<<"please input the search data: ";
cin>>data;
m=Search(arry,n,data);
cout<return 0;
}