【C++】函数模板 不同类型数组的运算
1 #include <iostream> 2 3 using namespace std; 4 5 template<class T>void index(int n,T num1[]){ 6 for(int i=0;i<n;i++) 7 { 8 cout<<"index"<<i<<endl; 9 cin>>num1[i]; 10 }; 11 } 12 13 template <class T>void print1(int n,T num1[]){ 14 for(int i=0;i<n;i++) 15 { 16 cout<<i<< " "<<num1[i]<<endl; 17 }; 18 } 19 20 template<class T> void print2(int n,T num1[]){ 21 for(int i=0;i<n;i++) 22 { 23 cout<<i<< " "<<num1[i]<<endl; 24 }; 25 } 26 27 template<class T>void rank(int n,T num1[]){ 28 T k; 29 for (int i=0;i<n-1;i++) 30 { 31 for(int j=i+1;j<n;j++){ 32 if(num1[i]>num1[j]) 33 { 34 k=num1[i]; 35 num1[i]=num1[j]; 36 num1[j]=k; 37 38 }; 39 } 40 }; 41 } 42 43 int main() 44 { 45 cout << "Hello world!" << endl; 46 int n; 47 int num1[20]; 48 float num2[20]; 49 double num3[30]; 50 cout<<"total arry"<<endl; 51 cin>>n; 52 index(n,num1); print1(n,num1); rank(n,num1); cout<<endl; print2(n,num1); 53 index(n,num2); print1(n,num2); rank(n,num2); cout<<endl; print2(n,num2); 54 index(n,num3); print1(n,num3); rank(n,num3); cout<<endl; print2(n,num3); 55 return 0; 56 }