2026/10/10 14:42:28

面向对象程序设计-第一阶段补充-数组排序输出(函数模板)

面向对象程序设计-第一阶段补充-数组排序输出(函数模板) 作者 何振峰单位 福州大学对于输入的每一批数按从小到大排序后输出。一行输入为一批数第一个输入为数据类型1表示整数2表示字符型数3表示有一位小数的浮点数4表示字符串0表示输入结束第二个输入为该批数的数量size0size10接下来为size个指定类型的数据。输出将从小到大顺序输出数据。函数接口定义sort函数将接受size个数据将它们从小到大排序后存在a指向的一段连续空间中。template class T void sort(T *a, int size)裁判测试程序样例#include iostream #include string using namespace std; /* 请在这里填写答案 */ template class T void display(T* a, int size){ for(int i0; isize-1; i) couta[i] ; couta[size-1]endl; } int main() { const int SIZE10; int a[SIZE]; char b[SIZE]; double c[SIZE]; string d[SIZE]; int ty, size; cinty; while(ty0){ cinsize; switch(ty){ case 1:sort(a,size); display(a,size); break; case 2:sort(b,size); display(b,size); break; case 3:sort(c,size); display(c,size); break; case 4:sort(d,size); display(d,size); break; } cinty; } return 0; }输入样例1 3 3 2 1 2 2 a A 3 3 1.5 2.6 2.2 4 2 bca abc 0输出样例1 2 3 A a 1.5 2.2 2.6 abc bca参考示例templateclass T void sort(T *a,int size){ for(int i0;isize;i){ cina[i]; } for(int i0;isize-1;i){ for(int j0;jsize-1-i;j){ if(a[j]a[j1]){ T ta[j]; a[j]a[j1]; a[j1]t; } } } }