Dimensional array implementation (Array, C ++ version)

xiaoxiao2021-03-06  82

#ifndef __ARRAY_H __ # Define __ARRAY_H__ 1

#include

Extern "C" {void exit (int);}

Template class array {private: int fsize; // array size T * alist; // Pointer to point to the first element of the array / / / {ALIST = New T [ FSIZE]; if (! Alist) {CERR << "Memory Allocation Error!" << Endl; Exit (1);}} public: // Constructor Array (int SZ = 50) {IF (SZ <= 0) SZ = 1; fsize = sz; allocate ();} // copy constructor: copy dynamic array x array (const array & x) {fsize = x.FSize; allocate (); for (int i = 0; i 0) {delete [] alist; fsize = 0;} } // Return Array Size INT LISTSIZE () {Return fsize;} // Assault operator's overload array & x) {if (fsize> 0) delete [] alist ; Fsize = x.fsize; allocate (); for (int i = 0; i fsize) {CERR << "Invalid Index!" << Endl; EXIT (1);} Return Alist [i];} // Type Conversion Operator T * () const {return anlist } // Modify the array size void resize (newsize <= 0) {CERR << "Invalid Array Size." << end1; return;} if (newsize! = Fsize) {t * newlist = New t [newsize]; if (! newlist) {CERR << "Memory Allocation Error!" << endl;

EXIT (1);} int N = (newsize <= fsize? newsize: fsize); // copy array for (int i = 0; i #include #include "array.h"

Void main () {array a (10), b (a), c; // Test INT N, I, COUNT = 0; cout << "list size:" << a.Listsize << Endl; A [5] = 30; B [5] = 50; cout << "a [5] =" << a [5] << endl; cout << "b [5] =" < > n; for (i = 5; i <= n; i ) {if (count == a.Listsize ()) a .Resize (count 10); // resize () test IF (I% 5 == 0 | i% 7 == 0) a [count ] = i;} // output array for (i = 0; i

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

New Post(0)