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
Template
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 INT D_MEM = size / m_dimsize; for (int i = 0; i } T * Operator & () {Return & (M_Parrays [0]); Carray 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 }; template 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 ~ 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 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;}