• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            一,利用模板來定制N維網格,這在開發一些與棋類有關的游戲等時幫助很大,因為是可擴展的,而且可以重用。以下摘抄其主要的代碼片斷。
            #ifndef NDGRID_H
            #define NDGRID_H

            template <typename T, int N>
            class NDGrid
            {
            public:
            ?NDGrid();
            ?NDGrid(int inSize);
            ?NDGrid(const NDGrid<T,N>& src);
            ?~NDGrid();

            ?NDGrid<T,N>& operator=(const NDGrid<T,N>& rhs);
            ?void resize(int newSize);
            ?NDGrid<T,N-1>& operator[](int x);
            ?const NDGrid<T,N-1>& operator[](int x) const;
            ?int getSize() const {return mSize; }
            ?static const int kDefaultSize = 10;
            protected:
            ?void copyFrom(const NDGrid<T,N>& src);
            ?NDGrid<T,N-1>* mElems;
            ?int mSize;
            };

            template <typename T>
            class NDGrid<T,1>
            {
            public:
            ?NDGrid(int inSize = kDefaultSize);
            ?NDGrid(const NDGrid<T,1>& src);
            ?~NDGrid();
            ?NDGrid<T,1>& operator=(const NDGrid<T,1>& rhs);
            ?void resize(int newSize);
            ?T& operator[](int x);
            ?const T& operator[](int x) const;
            ?int getSize() const { return mSize; }
            ?static const int kDefaultSize = 10;
            protected:
            ?void copyFrom(const NDGrid<T,1>& src);
            ?T* mElems;
            ?int mSize;
            };

            #endif


            #include "NDGrid.h"

            template <typename T,int N>
            const int NDGrid<T,N>::kDefaultSize;

            template <typename T,int N>
            NDGrid<T,N>::NDGrid(int inSize) : mSize(inSize)
            {
            ?mElems = new NDGrid<T,N-1>[mSize];
            ?for(int i = 0; i < mSize; i++){
            ??mElems[i].resize(inSize);
            ?}
            }

            template <typename T, int N>
            NDGrid<T,N>::NDGrid() : mSize(kDefaultSize)
            {
            ?mElems = new NDGrid<T,N-1>[mSize];
            }

            template <typename T, int N>
            NDGrid<T,N>::NDGrid(const NDGrid<T,N>& src)
            {
            ?copyFrom(src);
            }

            template <typename T, int N>
            NDGrid<T,N>::~NDGrid()
            {
            ?delete [] mElems;
            }

            template <typename T,int N>
            void NDGrid<T,N>::copyFrom(const NDGrid<T,N>& src)
            {
            ?mSize = src.mSize;
            ?mElems = new NDGrid<T,N-1>[mSize];
            ?for(int i = 0; i < mSize; i++){
            ??mElems[i] = src.mElems[i];
            ?}
            }

            template <typename T,int N>
            NDGrid<T,N>& NDGrid<T,N>::operator =(const NDGrid<T,N>& rhs)
            {
            ?//check for self-assignment
            ?if(this == &rhs){
            ??return (*this);
            ?}
            ?//free the old memory
            ?delete [] mElems;
            ?//copy the new memory
            ?copyFrom(rhs);
            ?return (*this);
            }

            template <typename T,int N>
            void NDGrid<T,N>::resize(int newSize)
            {
            ?//////allocate the new array iwth the new size
            ?NDGrid<T,N-1>* newElems = new NDGrid<T,N-1>[newSize];
            ?//copy all the elements, handling the cases where new Size
            ?//is larger than mSize nd smaller than msize.
            ?for(int i = 0; i < newSize && i < mSize; i++){
            ??newElems[i] = mElems[i];
            ??//resize the nested Grid elems recursively.
            ??newElems[i].resize(newSize);
            ?}
            ?//store the new size and pointer to the new array.
            ?//free the memory for the old array first.
            ?mSize = newSize;
            ?delete [] mElems;
            ?mElems = newElems;
            }

            template <typename T,int N>
            NDGrid<T,N-1>& NDGrid<T,N>::operator [](int x)
            {
            ?return (mElems[x]);
            }

            template <typename T,int N>
            const NDGrid<T,N-1>& NDGrid<T,N>::operator [](int x) const
            {
            ?return (mElems[x]);
            }

            //////specialization? implementation

            template <typename T>
            const int NDGrid<T,1>::kDefaultSize;

            template <typename T>
            NDGrid<T,1>::NDGrid(int inSize) : mSize(inSize)
            {
            ?mElems = new T[mSize];
            }

            template < typename T>
            NDGrid<T,1>::NDGrid(const NDGrid<T,1>& src)
            {
            ?copyFrom(src);
            }

            template <typename T>
            NDGrid<T,1>::~NDGrid()
            {
            ?delete [] mElems;
            }

            template <typename T>
            void NDGrid<T,1>::copyFrom(const NDGrid<T,1>& src)
            {
            ?mSize = src.mSize;
            ?mElems = new T[mSize];
            ?for(int i = 0; i < mSize; i++){
            ??mElems[i] = src.mElems[i];
            ?}
            }

            template < typename T>
            NDGrid<T,1> & NDGrid<T,1>::operator =(const NDGrid<T,1>& rhs)
            {
            ?//check for self-assignment
            ?if(this == & rhs){
            ??return(*this);
            ?}
            ?delete [] mElems;
            ?copyFrom(rhs);
            ?return (*this);
            }

            template <typename T>
            void NDGrid<T,1>::resize(int newSize)
            {
            ?T* newElems = new T[newSize];

            ?for(int i =0; i < newSize && i < mSize; i++){
            ??newElems[i] = mElems[i];
            ??///don't need to resize recursively,because this is the base case.
            ?}
            ?mSize = newSize;
            ?delete [] mElems;
            ?mElems = newElems;
            }

            template <typename T>
            T& NDGrid<T,1>::operator [](int x)
            {
            ?return (mElems[x]);
            }

            template <typename T>
            const T& NDGrid<T,1>::operator [](int x) const
            {
            ?return (mElems[x]);
            }



            #include <iostream>
            using namespace std;
            #include "NDGrid.h"

            int main()
            {
            ?NDGrid<int,3> my3DGrid;
            ?my3DGrid[2][1][2] = 5;
            ?my3DGrid[1][1][1] = 5;

            ?NDGrid<float,2> my2DGrid;
            ?my2DGrid[5][2] = 3;
            ?my2DGrid[2][5] = 2;

            ?cout << my3DGrid[2][1][2] << endl;

            ?cout<<my2DGrid[5][2]<<endl;

            ?return 0;
            }

            posts - 15, comments - 2, trackbacks - 0, articles - 29

            Copyright © 美洲豹

            国产福利电影一区二区三区,免费久久久久久久精 | 久久夜色精品国产网站| 精品国产99久久久久久麻豆| 久久综合偷偷噜噜噜色| 精品国产青草久久久久福利| 久久久久AV综合网成人| 伊人久久免费视频| 婷婷久久综合九色综合九七| 亚洲人成伊人成综合网久久久| 国产产无码乱码精品久久鸭| 青青热久久综合网伊人| 久久影院亚洲一区| 久久精品人人做人人爽97| 国产精品九九久久免费视频| 久久久久久国产精品无码下载| 人妻无码αv中文字幕久久 | 国产激情久久久久影院小草| 99久久综合国产精品免费| 欧美激情精品久久久久| 77777亚洲午夜久久多喷| 91精品国产91热久久久久福利| 国产一区二区久久久| 亚洲国产精品人久久| 亚洲精品乱码久久久久久按摩| 国产精自产拍久久久久久蜜| 人妻精品久久久久中文字幕69| 久久综合成人网| 国产激情久久久久影院老熟女免费| 中文无码久久精品| 理论片午午伦夜理片久久| 99久久国产综合精品成人影院| 久久久久亚洲AV片无码下载蜜桃 | 久久综合久久综合九色| 久久Av无码精品人妻系列| 精品久久久无码21p发布| 亚洲精品国产综合久久一线| 久久久久国产视频电影| 国产精品伊人久久伊人电影| 99热成人精品免费久久| 亚洲国产天堂久久综合网站| 曰曰摸天天摸人人看久久久|