如何用CArray类构造一个二维数组,并对其赋值和实现对成员的访问?CArray <CPoint, CPoint&> m_cPtArray;m_cPtArray.SetSize(10,10);for (int i=0; i<10; i++)
     for (int j=0; j<10; j++)
    {
...
     
     }
中间的代码怎么写?貌似用m_cPtArray[i][j]对数组成员访问不行!该怎么实现,望高手指点!!

解决方案 »

  1.   

    可以用CArray类的成员函数来访问其內容
      

  2.   

    如果是一维数组,因为重载的有[]运算符,可以直接m_cPtArray[i],这样来访问,那二维的为什么不能用类似的m_cPtArray[i][j]这样的形式去访问呢?
      

  3.   


    int i=0,j=0;
    int d1 = 3; //第1维是3
    int d2 = 5; //第2维是5
    typedef CArray<int,int&> INTARR;
    CArray<INTARR,INTARR&> m_2DArr;
    m_2DArr.SetSize(3); //建立2维数组
    for (i=0;i<d1;i++)
    m_2DArr[i].SetSize(d2); for(i=0;i<3;i++) //设初值
    for (j=0;j<5;j++)
    m_2DArr[i][j] = i*j; for(i=0;i<3;i++){ //输出测试
    for (j=0;j<5;j++)
    TRACE("%d ",m_2DArr[i][j]);
    TRACE("\n");
    }
    代码测试过,是可以的.
      

  4.   

    TRACE 宏是用在 VC6 中的,如果你是其他编译器,可以用其他输出办法试.
      

  5.   

    Supports arrays that are similar to C arrays, but can dynamically shrink and grow as necessary.Array indexes always start at position 0. You can decide whether to fix the upper bound or allow the array to expand when you add elements past the current bound. Memory is allocated contiguously to the upper bound, even if some elements are null.As with a C array, the access time for a CArray indexed element is constant and is independent of the array size.