您的位置:首页 > 编程语言 > C语言/C++

模板实现任意维数组

2007-01-14 18:10 239 查看
 

在OOT课程中老师给出了模板实现的2维数组,我受到启发写了一个实现任意维数组的模板。它与C++中默认的模板很像,并且能够检查越界。


//MyArray.cpp


//by jefferyzb @ 07.01.14


//OOT 2001_No.3






#include <iostream.h>


#include <stdarg.h>




//original 2-demission array template




template <class T> class Array;






template <class T> class ArrayTmp ...{ // 第二层类型


    friend class Array<T>;


    T* tpBody;


    int iRows, iColumns, iCurrentRow;


    


    ArrayTmp(int iRsz, int iCsz)




    ...{ 


        tpBody = new T[iRsz*iCsz]; 


        iRows = iRsz; 


        iColumns = iCsz;


        iCurrentRow = -1; 


    }


    


public:


    T& operator[ ](int j) 




    ...{ 


        return tpBody[iCurrentRow + j * iRows]; 


    }


};






template <class T> class Array ...{ // 第一层类型


    ArrayTmp<T> tTmp; // 第一层类型与第二层类型的关联


    


public:




    Array(int iRsz, int iCsz) : tTmp(iRsz, iCsz) ...{}


    


    ArrayTmp<T>& operator[ ](int i)




    ...{ 


        tTmp.iCurrentRow = i; 


        return tTmp; 


    }


};




/**////////////////////////////////////////////////////////////


//modified n-demission array template




int cnt = 1;




class Integer




...{


    


public:


    int n;




    Integer(int i=cnt) 




    ...{


        n = i;


        cnt++;


    }




    inline friend ostream & operator << (ostream& o, const Integer& output)




    ...{


        o <<"Integer:"<<output.n;


        return o;


    }


};




template <class T> class ArrayBase 




...{ 


    T* tpBody;


    int demision,curDemision;


    int * ipL;


    int length;


    int index;




public:




    ArrayBase(int demision, ...)




    ...{ 


        this->demision = demision;


        this->curDemision = 0;


        this->ipL = new int[demision];


        this->length = 1;


        this->index = 0;


        


        int iL;


        va_list ap;


        va_start ( ap, demision );


        for ( int i= 0; i< demision; i++ )




        ...{


            iL = va_arg (ap, int);


            this->ipL[i] = iL;


            length *= iL;


        }


        va_end (ap);




        this->tpBody = new T[length];


    }


    


    


    ArrayBase& operator[ ](int j) 




    ...{ 


        //refresh when reach the last brackets


        if(++curDemision > demision)




        ...{


            this->curDemision = 1;


            this->index = 0;


        }




        int size = 1;


        


        for (int i=demision-1; i>curDemision-1; i-- )




        ...{


            size *= this->ipL[i];


        }




        this->index += j * size;


    


        //check


        if(this->index >= this->length)




        ...{


            cout<<"Array out of bound ";


            this->index = 0;


        }




        return *this; 


    }


    


    ArrayBase & operator=(T t)




    ...{


        *(tpBody+index) = t;


        return *this;


    }


    


    friend ostream & operator << (ostream& o, const ArrayBase& output)




    ...{


        T * t = output.tpBody;


        t += output.index;


        o<<*t;


        return o;


    }


};








void main()




...{


    ArrayBase<int> a(4,2,3,4,5);


    a[1][2][3][4] = 9;


    cout<<a[1][2][3][4]<<endl;




    ArrayBase<Integer> b(4,2,3,4,5);


    b[1][2][3][4] = Integer(11);


    cout<<b[1][2][3][4]<<endl;


}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  integer class output c++
相关文章推荐