#include <iostream> #include <string> using namespace std; template<typename T,int size> class Data { T data[size]; public: Data() { cout << "input " << size << "datas:" << endl; for (int i = 0; i < size; i++) { cin >> data[i]; } } void show() { cout << size << " datas:" << endl; for (int i = 0; i < size; i++) { cout << data[i] << "\t"; } cout << endl; } T getMax(); void insort(); }; //输出最大值 template<typename T,int size> T Data<T, size>::getMax() { T max = data[0]; for (int i = 1; i < size; i++) { if (data[i] > max) { max = data[i]; } } return max; } //直接插入排序 template<typename T, int size> void Data<T, size>::insort() { T temp; int i = 0, j = 0; for (i = 1; i < size; i++) { // 数组的下标是从0开始的 // 这里从第二个数开始枚举 即假定第一个数是有序的 temp = data[i]; j = i; // 这里temp 临时储存每一次需要排序的数 while (j >= 1 && temp < data[j - 1]) { data[j] = data[j - 1]; j--; } data[j] = temp; } } int main() { Data<int, 5> d; d.show(); string lie(30,'-'); cout << lie << endl; cout << d.getMax() << endl; cout << "after sort:" << endl; d.insort(); d.show(); system("pause"); return 0; }标签:temp,int,Data,float,max,data,模板,size From: https://www.cnblogs.com/smartlearn/p/16944533.html