[Header file] C ++ implementation array

zhaozj2021-02-16  61

#ifndef ARRAY_H # define ARRAY_H # include using namespace std; template class array {public: // Constructor collection array (int sz = MIN); array (const array &); array ( Const elemType *, const int); // operator overload set const bool operator == (const array &) const; const boau = (const array &) const; const array operator = (Const Array) &); ELMTYPE & OPERATOR [] (Const INDEX);

// Built-in method const INT size () const; const; const ele min () const;

Private: // Private function void array_init (const eLEMTYPE * AP, Const Int SZ); // Initialization // Private Data ELMTYPE * IA; // Array Head Pointer INT _SIZE; // Array Length};

// Array implementation code // Private functions Template Void array :: array_init (const ele mType * ap, const Int SZ) {_size = SZ; IA = new elemType [_size]; for (int i = 0; i <_Size; i) {if (! AP) IA [i] = 0; ELSE IA [i] = AP [i];}

} // public function // Constructor set Template Array :: array (int SZ) {Array_init (0, SZ);} template array :: array (const array) & a) {Array_init (A.ia, A._SIZE);} Template Array :: array (const elemtype * ay, const Int sz) {Array_Init (Ay, SZ);}

// Method Collection Template Const EleMTYPE ARRAY :: Max () const {elemTyPE * MAX = IA; for (int = 0; ix <_size; ix) Max = * Max const elemType array :: min () const {elemTyPE * min = ia; for (int = 0; ix <_size; IX) min = * min> IA [iv]? IA IX: min; return * min;} template const Int array :: size () const {return_size;} // operator Collection Template ELMTYPE & Array :: Operator [] (const INDEX) {ask (index> = 0 && index <= _size); return ia [index];}

Template Const Array Array :: Operator = (const array & a) {_size = a._size; = new elemType [_size]; for (int i = 0; i < = a._size; i) IA [i] = a.ia [i];

Return * this;}

Template Const Bool Array :: Operator == (Const Array & A) const {bool x = true; try {i (_size! = a._size) throw x; for (int i = 0; I <= _size; i) IF (IA [i]! = A.ia [i]) throw x;} catch (bool x) {returnix x = false;}

Return X;}

Template Const Bool Array :: Operator! = (Const Array & A) const {bool x = true; try {f (_size == a._size) throw x; for (int i = 0; i <= _Size; I) IF (IA [i] == A.ia [i]) throw x;} catch (bool x) {return x = false;} returnix}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-16068.html

New Post(0)