热情软件屋

 

怎样用vc++5.0中的类创建一个二维动态数组


编号:QA002474
建立日期: 2000年1月23日 最后修改日期:2002年6月5日
所属类别:

赵星:
    编程工具: vc++5.0
    操作系统: win98
    问题: 我由于不知要建数组的大小,希望创建一个二维动态数组,请问怎样用vc++5.0中的类如CArray, 创建一个二维动态数组?

回答:

    假设二维数组的元素是 Class T, 则可生成一个 template <class T>的动态二维数组。 必须注意以下几点。
    1,必须自定义一个以行数、列数为参数的构造函数。
    2,必须自定义析构函数,以保证在析构该二维数组类的同时确实清除所有元素的内存空间。
    3,为了使用方便,建议重载括号操作符,实现对元素的引用。
    4, 建议查看 CArray 的源码,以确认每个步骤。
    下面是基本框架:
    头文件:
    #include <afxtempl.h>
    template <class T> class C2dArray: public CArray<CArray<T, T&>, CArray<T,T&>&>
    {
    protected:
     short row, col;
    public:
     C2dArray();
     C2dArray(short row, short col);
     SetSize(short row, short col);
     virtual ~C2dArray();
    
     T& operator () (short row, short col); // 重载括号操作
    
    };
    
    template <class T> inline T& C2dArray<T>::operator () (short row, short col)
    {
    .....
    }
    
    CPP 文件:
    template <class T> C2dArray<T>::C2dArray()
    { SetSize(0,0);}
    template <class T> C2dArray<T>::C2dArray(short row, short col)
    { SetSize(row,col);}
    template <class T> C2dArray<T>::SetSize(short row, short col)
    {
    .....
    }
    template <class T> C2dArray<T>::~C2dArray()
    {
    ...
    }
    C++ 的 new 操作直接支持多维数组的申请。详情可参考 new 的联机帮助。
    new 支持多维数组的申请, 但仅对最高维是动态的,较低维的各维的长度必须是常数。本人编写了两个动态多维数组申请和释放的函数。 分别为mnew 和 mdelete. 代码如下。注意,其中可变参数列表中的数必须是长整型的。第二个参数 dimention 表示的是要申请的数组的维数,后面就是各维所对应的长度。
     char * mnew (unsigned long typesize, unsigned short dimention, ...) /*=============================================================================
     by Kan ZENG, 2000.02.16. tested on 2000.02.16
    
     mnew and mdelete are couple of routines to deal with allocing and freeing
     the memory for multi-dimention array.
     para:
     typesize -- the size in byte of one element
     dimention - the number of dimention
     ... - the variational argument list. each one MUST BE long interger.
     The further right, the lower the dimention is.
    
     p - the pointer which was alloced by mnew
    
     Return:
     mnew return the first address of alloced memory block if successful or NULL
     if fail.
    
     Usage:
     if a double type 3D dynamical array is required, the method to using mnew as
     following:
     double ***myarray =
     (double ***) mnew(sizeof(double),3, (long)n3, (long)n2, (long)n1);
    
     After that, you can refer the element as usual:
     e.g. myarray[4][2][5]=45.77;
     =============================================================================*/
     {
     va_list vl;
     long i, j;
     long prdt1, prdt2;
     long *dim;
     char *res;
    
     if (dimention <1) return NULL;
    
     dim= new long[dimention];
    
     // dimention is the last argument specified; all
     // others must be accessed using the variable-
     // argument macros.
     va_start( vl, dimention);
    
     // Step through the list.
     for(i=dimention-1; i>=0; i--)
     dim[i]=va_arg( vl, long );
     va_end( vl );
    
     if (dimention==1) {
     res = new char[dim[0]];
     delete[] dim;
     return res;
     }
    
     prdt1=dim[1]; prdt2=dim[0]*dim[1];
     for(i=2; i     ++prdt1 *= dim[i];
     prdt2 *= dim[i];
     }
    
     if ((res=new char[prdt1*sizeof(void *)+prdt2*typesize])==NULL) {
     delete[] dim;
     return NULL;
     }
    
     char *p;
     void **q2, **p1, **p2;
    
     p = res+prdt1*sizeof(void *) ;
     q2 = (void **)p;
     prdt2 /= dim[0];
     q2 -= prdt2;
    
     p2 = q2;
     for(j=0; j     *p2=p;
     p2++;
     p+=dim[0]*typesize;
     }
    
     for(i=1; i<(dimention-1); i++) {
     p1=q2;
     prdt2 /= dim[i];
     q2 -= prdt2;
     p2 = q2;
     for(j=0; j     *p2=p1;
     p2++; p1+=dim[i];
     }
     }
    
     delete[] dim;
     return res;
     }
    
     void mdelete(void * p)
     {
     if (p==NULL) return;
     delete[] (char *)p;
     p=NULL;
     }
    
    kaffir的意见:
    #define matrix_allocate(matrix, width, height, TYPE) {\
     matrix = new TYPE* [height];\
     for(int _i = 0; _i < height; _i++)\
     matrix[_i] = new TYPE[width];\
    }
    #define matrix_delete(matrix, width, height){\
     for(int _i = 0; _i < height; _i++)\
     delete [] matrix[_i];\
     delete [] matrix;\
     matrix = 0;\
    }
    用这两个宏吧 很爽的 hoho~~

此问题由曾侃回答。

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio