算法复习--------------二维数组
来源:互联网 发布:最好的聊天交友软件 编辑:程序博客网 时间:2024/06/07 14:01
二维数组的实现是建立在一个数组的基础上的,通过一个一维数组的指针来保存二维数组的行, 但是可以通过cols来控制每一行的个数
具体实现代码如下:
#include "ClassArray1D.h"template<class T>class Array2D{private:int rows, cols;Array1D<T> * row; public:Array2D(int r = 0, int col = 0); //构造函数Array2D(const Array2D<T> & v);//复制构造函数~Array2D(); //析构函数int GetRows()const{ return rows; } //获取行int GetCols()const{ return cols; } //获取列Array1D<T>& operator[](int i)const; //下表操作,获取第几行Array2D<T>& operator=(const Array2D<T> &m); Array2D<T> operator+()const;Array2D<T> operator+(const Array2D<T>& m)const;Array2D<T> operator-()const;Array2D<T> operator-(const Array2D<T>& m)const;Array2D<T> operator*(const Array2D<T>& m)const;Array2D<T> operator+=(const T& x);};template<class T>Array2D<T>::Array2D(int r, int c){if (r < 0 || c < 0)throw;if ((!r || !c) && (r || c))throw;rows = r;cols = c;row = new Array1D<T>[r];for (int i = 0; i < r; i++){row->ReSize(c);}}template<class T>Array2D<T>::Array2D(const Array2D<T>& m){rows = m.rows;cols = m.clos;row = new Array1D<T>[rows];for (int i = 0; i < rows; i++){row[i] = m.row[i];}}template<class T>Array1D<T>& Array2D<T>::operator[](int i)const{if (i < 0 || i >= rows) throw;return row[i];}template<class T>Array2D<T> Array2D<T>::operator-(const Array2D<T>& m)const{if (rows != m.rows || cols != m.cols) throw;Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){w.row[i] = row[i] - m.row[i];}return w;}template<class T>Array2D<T> Array2D<T>::operator+(const Array2D<T>& m)const{if (rows != m.rows || cols != m.cols) throw;Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){w.row[i] = row[i] + m.row[i];}return w;}template<class T>Array2D<T> Array2D<T>::operator-()const{Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){w.row[i] = -row[i];}return w;}template<class T>Array2D<T> Array2D<T>::operator+()const{Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){w.row[i] = row[i];}return w;}template<class T>Array2D<T> Array2D<T>::operator+=(const T& x){Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){w.row[i] = row[i] + x;}return w;}template<class T>Array2D<T> Array2D<T>::operator*(const Array2D<T>& m)const{if (cols != m.cols)throw;Array2D<T> w(rows, cols);for (int i = 0; i < rows; i++){for (int j = 0; j < m.cols; j++){T sum = (*this)[i][0] * m[0][j];for (int k = 1; k < cols; k++)sum += (*this)[i][k] * m[k][j];w[i][j] = sum;}}return w;}
0 0
- 算法复习--------------二维数组
- 二维数组的指针复习
- 二维数组查找算法
- java复习——二维数组
- 算法复习之------树状数组
- 二维数组螺旋式打印算法
- 【算法】二维数组中的查找
- [算法]二维数组循环输出
- 二维数组查找算法(C++)
- 算法-二维数组中的查找
- 复习指针数组以及如何把二维数组赋值给二维指针
- 算法复习--------------一维数组类
- Java数组常用排序算法实现复习
- 算法复习:旋转数组的最小数字
- 用二维数组编写的银行家算法
- 去掉二维数组重复值[算法]
- 一道二维数组的算法题
- 二维数组排序 n种算法
- django开发注意点
- p-namespace
- HTML 基础控件介绍
- .NET开发不可错过的25款必备工具
- IOS开发语言Swift入门连载---方法
- 算法复习--------------二维数组
- Qt控件如何响应QEvent
- ORACLE操作表时弹出提示框:”资源正忙,需指定nowait"的解锁方法
- IM(INSTANTMESSAGING) 服务器的架构相关文章
- TCP三次握手中SYN,ACK,Seq三者的关系
- 项目使用到的组件
- 2.30
- linux下USB驱动移植
- 第7周项目1(3)