标签:int res char src har auto getc std for
在GameBoard,h文件:
#ifndef _GAMEBOARD_H #define _GAMEBOARD_H #include <iostream> #include <vector> template<typename T> class GameBoard { public: GameBoard(); GameBoard(const T& src); //复制构造函数 GameBoard(size_t inWidth,size_t inHeight); virtual ~GameBoard(); public: void setElemAt(size_t x,size_t y,const T& inElem); const T& getElem(size_t x,size_t y); size_t getWidth(){return mWidth;} size_t getHeight(){return mHeight;} static const size_t mDefaultWidth = 10; static const size_t mDefaultHeight = 10; private: size_t mWidth,mHeight; void copyFrom(const T& src); void initialContainer(); std::vector<std::vector<T>> mCells; }; #endif //_GAMEBOARD_H
在GameBoard.cpp文件里:
#include "GameBoard.h" template<typename T> GameBoard<T>::GameBoard() :mWidth(0) ,mHeight(0) { initialContainer(); } template<typename T> GameBoard<T>::GameBoard(const T& src) :mWidth(src.mWidth) ,mHeight(src.mHeight) { copyFrom(src); } template<typename T> GameBoard<T>::GameBoard(size_t inWidth,size_t inHeight) :mWidth(inWidth > mDefaultWidth ? mDefaultWidth : inWidth) ,mHeight(inHeight > mDefaultHeight ? mDefaultHeight : inHeight) { initialContainer(); } template<typename T> GameBoard<T>::~GameBoard() { } template<typename T> void GameBoard<T>::setElemAt(size_t x,size_t y,const T& inElem) { mCells[x][y] = inElem; } template<typename T> const T& GameBoard<T>::getElem(size_t x,size_t y) { return mCells[x][y]; } template<typename T> void GameBoard<T>::copyFrom(const T& src) { mWidth = src.mWidth; mHeight = src.mHeight; initialContainer(); for(int i = 0;i < mWidth;++i) { for(int j = 0;j < mHeight;++j) { mCells[i][j] = src.mCells[i][j]; } } } template<typename T> void GameBoard<T>::initialContainer() { mCells.resize(mWidth); for(auto &item : mCells) { item.resize(mHeight); } }
在main函数里:
#include <iostream> #include <initializer_list> #include "GameBoard.h" #include "GameBoard.cpp" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { GameBoard<int> myIntGameBoard(10,10); for(size_t i = 0;i < myIntGameBoard.getWidth();++i) { for(size_t j = 0;j < myIntGameBoard.getHeight();++j) { myIntGameBoard.setElemAt(i,j,i*10+j); } } for(size_t i = 0;i < myIntGameBoard.getWidth();++i) { for(size_t j = 0;j < myIntGameBoard.getHeight();++j) { std::cout << myIntGameBoard.getElem(i,j) << " "; } std::cout << std::endl; } return 0; }
其意思就是在mCells里插入数据0~99;并且打印出来;
标签:int res char src har auto getc std for
原文地址:https://www.cnblogs.com/boost/p/10354949.html