1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| #include<iostream> template<class T> class arrList{ private: int maxSize; int curLen; T* aList; bool check(int index){ if(curLen>maxSize || index<0 || index>curLen){ std::cout << "ERROR"; return false; } return true; } public: arrList(const int size):maxSize(size){ curLen = 0; aList = new T[maxSize]; } ~arrList(){ delete [] aList; } void show(){ int i=0; while(i<this->curLen){ std::cout << this->aList[i++] << " "; } std::cout << std::endl; } void clear(){ delete [] aList; curLen = 0; aList = new T[maxSize]; } int lenth(){ return curLen; } bool append(const T value){ if(check(0)){ aList[curLen++]=value; return true; } return false; } bool insert(int index,const T value){ if(check(index)){ for(int i=curLen+1;i>index;i--){ aList[i] = aList[i-1]; } aList[index] = value; curLen++; return true; } return false; } bool del(int index){ if(check(index)){ for(int i=index;i<curLen;i++){ aList[i] = aList[i+1]; } curLen--; return true; } return false; } T& operator[](int i){ if(check(i)){ return aList[i]; } return aList[0]; } bool setValue(int index,const T value){ if(check(index)){ aList[index] = value; return true; } return false; } bool getPos(int& index,const T value){ for(int i=0;i<curLen;i++){ if(aList[i]!=value) continue; index = i; return true; } index = -1; return false; } void arr_union(arrList& arrA,arrList& arrB){ int i = 0,index; bool flag; while(i<arrB.curLen){ flag = arrA.getPos(index,arrB[i]); if(!flag) arrA.append(arrB[i]); i++; } } void arr_intersection(arrList& arrA,arrList& arrB){ int i = curLen-1,index; bool flag; while(i>=0){ flag = arrB.getPos(index,arrA[i]); if(!flag) arrA.del(i); i--; } } };
|