• <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維網(wǎng)格,這在開發(fā)一些與棋類有關(guān)的游戲等時(shí)幫助很大,因?yàn)槭强蓴U(kuò)展的,而且可以重用。以下摘抄其主要的代碼片斷。
            #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;
            }


            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


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

            Copyright © 美洲豹

            午夜精品久久久久成人| 久久99国产精品久久99小说| jizzjizz国产精品久久| 浪潮AV色综合久久天堂| 爱做久久久久久| 久久久久黑人强伦姧人妻| 久久人人爽人人爽人人av东京热 | 国产成人精品久久亚洲高清不卡 | 国产成人精品综合久久久| 亚洲AV无码久久精品蜜桃| 中文字幕久久欲求不满| 亚洲av日韩精品久久久久久a| 精品久久久久久99人妻| 72种姿势欧美久久久久大黄蕉| 精品久久人人妻人人做精品| 9久久9久久精品| 精品综合久久久久久98| 久久精品国产一区二区三区不卡| 精品久久久久久国产潘金莲| 污污内射久久一区二区欧美日韩 | 国产成人精品免费久久久久| 亚洲人AV永久一区二区三区久久| 久久综合久久久| 久久青草国产手机看片福利盒子| 色婷婷综合久久久久中文一区二区 | 久久99中文字幕久久| 亚洲AV日韩精品久久久久久久| 亚洲а∨天堂久久精品9966| 99久久综合狠狠综合久久| 国产精品久久久久久影院| 婷婷久久久亚洲欧洲日产国码AV | 精品熟女少妇aⅴ免费久久| 国产精品久久免费| 久久久精品人妻一区二区三区四| 亚洲AV无码久久精品色欲| 99久久精品国产一区二区| 欧美成人免费观看久久| 国内精品综合久久久40p| 久久SE精品一区二区| 奇米影视7777久久精品| 无码精品久久久久久人妻中字|