📄 gridstring.h
字号:
#include "Grid.h" // The file containing the Grid template definition shown above#include <cstdlib>#include <cstring>using namespace std;template <int WIDTH, int HEIGHT>class Grid<char*, WIDTH, HEIGHT>{ public: Grid(); Grid(const Grid<char*, WIDTH, HEIGHT>& src); ~Grid(); Grid<char*, WIDTH, HEIGHT>& Grid<char*, WIDTH, HEIGHT>::operator=( const Grid<char*, WIDTH, HEIGHT>& rhs); void setElementAt(int x, int y, const char* inElem); char* getElementAt(int x, int y) const; int getHeight() const { return HEIGHT; } int getWidth() const { return WIDTH; } protected: void copyFrom(const Grid<char*, WIDTH, HEIGHT>& src); char* mCells[WIDTH][HEIGHT];};template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::Grid(){ for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { // initialize each element to NULL mCells[i][j] = NULL; } }}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::Grid(const Grid<char*, WIDTH, HEIGHT>& src){ copyFrom(src);}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>::~Grid(){ for (int i = 0; i < WIDTH; i++) { for (int j = 0; j < HEIGHT; j++) { delete [] mCells[i][j]; } }}template <int WIDTH, int HEIGHT>void Grid<char*, WIDTH, HEIGHT>::copyFrom( const Grid<char*, WIDTH, HEIGHT>& src){ int i, j; for (i = 0; i < WIDTH; i++) { for (j = 0; j < HEIGHT; j++) { if (src.mCells[i][j] == NULL) { mCells[i][j] = NULL; } else { mCells[i][j] = new char[strlen(src.mCells[i][j]) + 1]; strcpy(mCells[i][j], src.mCells[i][j]); } } }}template <int WIDTH, int HEIGHT>Grid<char*, WIDTH, HEIGHT>& Grid<char*, WIDTH, HEIGHT>::operator=( const Grid<char*, WIDTH, HEIGHT>& rhs){ int i, j; // check for self-assignment if (this == &rhs) { return (*this); } // free the old memory for (i = 0; i < WIDTH; i++) { for (j = 0; j < HEIGHT; j++) { delete [] mCells[i][j]; } } // copy the new memory copyFrom(rhs); return (*this);}template <int WIDTH, int HEIGHT>void Grid<char*, WIDTH, HEIGHT>::setElementAt( int x, int y, const char* inElem){ delete[] mCells[x][y]; if (inElem == NULL) { mCells[x][y] = NULL; } else { mCells[x][y] = new char[strlen(inElem) + 1]; strcpy(mCells[x][y], inElem); }}template <int WIDTH, int HEIGHT>char* Grid<char*, WIDTH, HEIGHT>::getElementAt(int x, int y) const{ if (mCells[x][y] == NULL) { return (NULL); } char* ret = new char[strlen(mCells[x][y]) + 1]; strcpy(ret, mCells[x][y]); return (ret);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -