Simulate the class of any dimension group

zhaozj2021-02-16  51

The following is a class that can simulate an arbitrary dimension group. The size can be specified. The disadvantage is that it is more troublesome and requires a dimension of a dimension.

I have already revised several times. Bug I didn't test. It is estimated that there is still. But the program is what it means. Interested in modify it. Modify if there is a sharing spirit. Also let NHSoft know.

My email xheartblue@etang.com

Basic energy simulation takes the address. Operator []. Allocation of any dimension.

And intelligent memory allocation. (Continuous in memory. Only one memory is allocated.)

#include "stdafx.h" #include #include using namespace std;

Template Class Carray {public: carray () {m_parrays = null; m_dimsize = 0; m_pdata = null;}

Carray (INT * DIMSIZE, T * PMEM = NULL) {m_pdata = null; Create (DIMSIZE, PMEM);

/ * Create * / void create (INT * DIMSIZE, T * PMEM = null) {int size = 1; m_pdata = NULL; // Memory is only created once IF (PMEM == NULL) {for (int i = 0; I

M_PARRAYS = New Carray [* DIMSIZE]; M_DIMSIZE = * DIMSIZE;

INT D_MEM = size / m_dimsize; for (int i = 0; i

}

T * Operator & () {Return & (M_Parrays [0]);

Carray & operator []Dex) {assert (index = 0); return m_parrays [index];

Void Free () {delete [] m_parrays; m_parrays = null; if (m_pdata! = null) {delete m_pdata; m_pdata = null; cout << "T type memory is released, the memory unified allocation" << Dim << endl }}

~ Carray () {free ();

Protected: Carray * m_parrays; int m_dimsize; t * m_pdata;

}; template Class Carray {public: carray () {m_data = null; m_dimsize = 0; m_bneeddelete = false;} carray (int * DIMSIZE, T * PMEM) {m_bneeddelete = false; CREATE (DIMSIZE, PMEM);} Void Create (INT Size, T * PMEM) {if (PMEM == NULL) {PMEM = New T [size]; m_bneeddelete = true;} m_data = pmem; m_dimsize = size;}

T * Operator & () {RETURN M_DATA;} Void Create (INT * DIMSIZE, T * PMEM) {if (PMEM == NULL) {PMEM = New T [DIMSIZE [0]]; m_bneeddelete = true;} m_data = PMEM ; M_dimsize = dimsize [0];} T & operator [} {assert (index = 0); return m_data [index];} void free () {if (m_bneeddelete) {delete [] m_data; m_data = null; cout << "T Type Memory is released" << endl;}}

~ Carray () {free ();

PUBLIC: T * m_data; int m_dimsize; bool m_bneeddelete;};

INT _TMAIN (int Argc, _tchar * argv []) {INT DIMSIZE [4] = {4, 6, 7, 7}; / * carray is 4 6 7 7 array * / carray Array; array .Create (dimsize); Array [2] [1] [3] [1] = 1; INT * PT = & (Array [2] [1] [3]); cout << "pt [0]:" << pt [1] << endl;

INT * PT2 = & Array [2] [1]; * pt2 = 234; cout << "array [2] [1] [0] [0]:" << array [2] [1] [0] [0 ] << Endl; cout << "array [2] [1] [3] [1]:" << array [2] [1] [3] [1] << endl; return 0;}

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

New Post(0)