类模板

liubingyu / 2023-05-04 / 原文

实现一个类模板,它可以接受一组数据,能对数据排序,也能输出数组的内容。

每行输入的第一个数字为0,1,2或3:为0时表示输入结束; 为1时表示将输入整数,为2时表示将输入有一位小数的浮点数,为3时表示输入字符。

如果第一个数字非0,则接下来将输入一个正整数,表示即将输入的数据的数量。

从每行第三个输入开始,依次输入指定类型的数据。

类模板:

 
template <class T>
class MyArray
 

裁判测试程序样例:

 
#include <iostream>
using namespace std;

/* 请在这里填写答案 */

template<class T>
MyArray<T>::~MyArray(){ delete[] data;}

template<class T>
bool MyArray<T>::check(){
    int i;
    for(i=0;i<size-1;i++)
        if(data[i]>data[i+1]) { cout<<"ERROR!"<<endl;return false;}
    return true;
}
int main( )
{
    MyArray<int> *pI;
    MyArray<float> *pF;
    MyArray<char> *pC;
    int ty, size;
    cin>>ty;
    while(ty>0){
        cin>>size;
        switch(ty){
            case 1: pI = new MyArray<int>(size);   pI->sort(); pI->check(); pI->display(); delete pI; break;
            case 2: pF = new MyArray<float>(size); pF->sort(); pF->check(); pF->display(); delete pF; break;
            case 3: pC = new MyArray<char>(size);  pC->sort(); pC->check(); pC->display(); delete pC; break;
        }
        cin>>ty;
    }
    return 0;
}


 

输入样例:

1 3 2 3 1
2 4 1.5 2.6 3.7 0.5
3 2 A a
0
 

输出样例:

1 2 3
0.5 1.5 2.6 3.7
A a
 1 template <class T>
 2     class MyArray
 3     {
 4 private:
 5         T*data;
 6         int size;
 7 public:
 8         MyArray(int size);
 9         bool check();
10         void sort();
11         void display();
12         ~MyArray();
13     };
14 template <class T>
15     MyArray<T>::MyArray(int size)
16     {
17 this->size=size;
18         data=new T[size];
19         for (int i=0;i<size;i++){
20             cin>>data[i];
21         }
22 
23         
24     }
25 template <class T>
26     void MyArray<T>::sort(){
27 for(int i=0;i<size;i++){for(int j=i+1;j<size;j++)
28 {
29 if (data[i]>data[j]){
30     T temp;
31     temp=data[i];
32     data[i]=data[j];
33     data[j]=temp;
34 }
35 }
36 }
37 }
38 template <class T>
39     void MyArray<T>::display(){
40 for (int i=0;i<size;i++){
41 
42     cout<<data[i];
43 
44     if (i==size-1)
45         cout<<endl;
46     else cout<<" ";
47 }
48 
49     
50     }