自定义c++二维动态数组

template <class T>
class myMat
{
public:
    myMat(int rows_,int cols_)
    {
        rows=rows_;cols=cols_;
        data=new T[rows*cols];
    }
    T& at(int r,int c)
    {
        T &p=*(data+r*rows+c);
        return p;
    }
    T& operator ()(int r,int c){return *(data+r*rows+c);}//获取r行、c列值
    T* operator [](int r){return (data+r*rows);}//获取第r行指针
    int rows,cols;
    T * data;
};

 

posted @ 2022-06-26 16:24  凤凰_1  阅读(19)  评论(0编辑  收藏  举报