第一次发现有人把 CArray<int, int> myArray 理解为二维数组。

解决方案 »

  1.   

    typedef CArray<int, int> CIntArray;
    typedef CArray<CIntArray, CIntArray&> CIntArray2;
    CIntArray2 ar2;
    ar2.SetSize(2);
    ar2[1].SetSize(2);
    ar2[2].SetSize(2);
    ar2[0][1]=10;
    ar2[1][1]=10;
      

  2.   

    typedef CArray<int, int> CIntArray;
    typedef CArray<CIntArray, CIntArray&> CIntArray2;
    CIntArray2 ar2;
    ar2.SetSize(2);
    ar2[1].SetSize(2); //ar2[0].SetSize(2);
    ar2[2].SetSize(2); //ar2[1].SetSize(2);
    ar2[0][1]=10;
    ar2[1][1]=10;
      

  3.   

    错了

    ar2[0].SetSize(2);
    ar2[1].SetSize(2);
      

  4.   

    to A_Qiao
    我从来都没有把数组的定义看成是二维数组!我知道它可以用于二维数组,但不知道如何写。现在终于知道了,谢谢各位!
    请问如何给各位加分的呢?
      

  5.   

    //---------------------------------------------------------------
    // FileName:matrix.h
    // This is the maxtrix class
    #include "iostream.h"
    #if !defined __MATRIX_H
    #define  __MATRIX_Hclass matrix
    {
    public:
    //constructor
    matrix(); //默认构造函数
    matrix(const matrix& x);          //在已有的矩阵基础上构造新的矩阵
    matrix(int row,int col);          //建立一个row行col列的空矩阵
        matrix(int *x,int row,int col);            //在二维数组的基础上构造新的矩阵
        matrix(const matrix& x,int row,int col);   //在已有的矩阵基础上改变其行和列,并建立一个新的矩阵
    //destructor
    ~matrix();
    //member functions
    int GetRow(void){return _row+1;}
    int GetColumn(void){return _col+1;}
        int GetValue(int row,int col); int SetValue(int value,int row,int col);   //if success,return 1,else return 0;
    int ChangeScale(int row,int col);          //if success,return 1,else return 0;//inorder to transfer the value
    matrix& operator =(const matrix& x);//friend members
    //下列运算尽管对错误有相应处理,不过建议用户自己在使用之前最好检查
    //--------------------------------------------------------------//
    //矩阵的标准乘法
    friend matrix operator*(const matrix& x,const matrix& y);   
    //矩阵的加法
    friend matrix operator+(const matrix& x,const matrix& y);//Protected members are declared here
    protected: int _row;                   //矩阵行数
    int _col;                   //矩阵列数
    int *_matrix;               //内部矩阵(即int指针)
    };ostream& operator<<(ostream& os,matrix& x);#endif
      

  6.   

    //----------------------------------------------------------------
    // apply for matrix class#include "matrix.h"//Constructors for establish a matrix instance
    matrix::matrix()
    {
            _row=0;
    _col=0;
    _matrix=new int[sizeof(int)];
            _matrix[0]=0;
    }matrix::matrix(const matrix& x)    //拷贝构造函数
    {
    _row=x._row;
    _col=x._col;
        _matrix=new int[(_row+1)*(_col+1)*sizeof(int)];
    long i;         //矩阵可能比较大
    for(i=0;i<(_row+1)*(_col+1);i++)
    _matrix[i]=x._matrix[i];
    }matrix::matrix(int *x,int row,int col)  //你必须先将二维数组转换为(int *)才行
    {

    _row=row-1;   //为了使其与定义形式一致,需要减一
        _col=col-1;
        _matrix=new int[(_row+1)*(_col+1)*sizeof(int)];
        long i;         //矩阵可能比较大
          for(i=0;i<(_row+1)*(_col+1);i++)
        _matrix[i]=x[i];
    }matrix::matrix(int row,int col)
    {

    if((row>0)&&(col>0))
    {
    _row=row-1;   //为了使其与定义形式一致,需要减一
    _col=col-1;
    _matrix=new int[(_row+1)*(_col+1)*sizeof(int)];
        long i;         //矩阵可能比较大
          for(i=0;i<(_row+1)*(_col+1);i++)
                 _matrix[i]=0;
    }
    else
    {
    _row=0;
    _col=0;
    _matrix=new int[sizeof(int)];
            _matrix[0]=0;
    }}matrix::matrix(const matrix& x,int row,int col)
    {
        if((row>0)&&(col>0))
    {
    _row=row-1;
    _col=col-1;
        _matrix=new int[(_row+1)*(_col+1)*sizeof(int)];
    long i;         //矩阵可能比较大
    for(i=0;i<(_row+1)*(_col+1);i++)
    _matrix[i]=x._matrix[i];
    }
    else
    {
            _row=0;
    _col=0;
    _matrix=new int[sizeof(int)];
            _matrix[0]=0;
    }
    }
    //destructor
    matrix::~matrix()
    {
        delete[] _matrix;
    }//member functions
    int matrix::GetValue(int row,int col)
    {
    if (_matrix!=0)
      if ((row<=_row)&&(col<=_col)) 
           return _matrix[row*(_col+1)+col];          //如果成功,返回值
     
        //cout<<"exceed the range"<<endl;   //如果失败,显示错误
    return 0;
    }int matrix::SetValue(int value,int row,int col)
    {
    if (_matrix!=0) 
       if ((row<=_row)&&(col<=_col)) 
       {
       _matrix[row*(_col+1)+col]=value; 
       return 1;  //success
       }
     return 0;      //false;}matrix& matrix::operator =(const matrix& x)
    {  
    if (this!=&x)
    {
    _row=x._row;
    _col=x._col;
    delete[] _matrix;
        _matrix=new int[(_row+1)*(_col+1)*sizeof(int)];
    long i;         //矩阵可能比较大
    for(i=0;i<(_row+1)*(_col+1);i++)
    _matrix[i]=x._matrix[i];
    }
        return *this;
    }
    int matrix::ChangeScale(int row,int col)
    {
    if((row>0)&&(col>0))
    {
    _row=row-1;
    _col=col-1;
    return 1;     //success
    }
    return 0;         //false}
    matrix operator+(const matrix& x,const matrix& y)
    {

    matrix tmp;
    if ((x._row!=y._row)||(x._col!=y._col))
    {
    //cout<<"error";
    return(tmp);
    }
    else
    {
            tmp._row=x._row;
    tmp._col=x._col;
        tmp._matrix=new int[(tmp._row+1)*(tmp._col+1)*sizeof(int)];
        long i;         //矩阵可能比较大
         for(i=0;i<(tmp._row+1)*(tmp._col+1);i++)
      tmp._matrix[i]=x._matrix[i]+y._matrix[i];
      return(tmp);
    }



    }matrix operator *(const matrix& x,const matrix& y)
    {
    if(x._col!=y._row)
    {
    //cout<<"error";
    matrix tmp;
    return tmp;
    }
    else
    {
    matrix tmp(x._row+1,y._col+1);
    int i,j,k;
    for(k=0;k<=x._row;k++)  //这里是矩阵A的行数
       for(j=0;j<=x._col;j++)  //这里是矩阵A的列数或B的行数
      for(i=0;i<=y._col;i++)  //这里是矩阵B的列数
         tmp._matrix[k*(y._col+1)+i]+=x._matrix[k*(x._col+1)+j]*y._matrix[j*(y._col+1)+i];
         return tmp;    
    }
    }ostream& operator<<(ostream& os,matrix& x)
    {
    int i,j;
    for(i=0;i<x.GetRow();i++)
    {
    cout<<"Row["<<i<<"]: ";
    for(j=0;j<x.GetColumn();j++)
    os<<x.GetValue(i,j)<<"       ";
    cout<<endl;
    }
    return os;
    }