c++作业总结:抽象矩阵及其迭代器的实现
题目解析:
矩阵的映射实质是内存空间的逻辑形式,使用一维数组将其实现。而迭代器起到一个中间媒介的作用,通过他把数组和Matrix类中算法关联起来,在STL简单、通用、高效的特性下解答。
问题关键:
关键在于用一维数组存储二维矩阵,假设有一个矩阵Matrix(m,n) ,index为矩阵中某个元素在一维数组的索引,那么我们只需利用公式 index = i * n + j 即可完成从二维矩阵到一维数组的映射。
迭代器最大的好处是 可以使容器和算法分离, 假设index 为矩阵中某一行首地址的索引,则公式为 index += n ,即可完成迭代器对抽象矩阵的首地址的迭代
代码展示:
#include <iostream>
using namespace std;
template <class T>
class Matrix {
T* Arry;
T* tail; //“哨兵”功能位于抽象矩阵m_x行加1的位置,起停止作用
public:
int m_x;
int m_y;
Matrix()
Arry = NULL;
tail = NULL;
m_x = m_y = 0;
Matrix(int x, int y) {
Arry = new T[x * y];
m_x = x;
m_y = y;
tail = Arry + m_x * m_y;//当最后一行首地址等于mid时,执行条件mid != end则不会遍历最后一行数据
void insertValue(int x, int y, T value)
int index = x * m_y + y;
Arry[index] = value;
T* Begin()
return Arry;
T* End()
return tail;
template<class T>
class MatrixIterator
T* m_T; //行首地址
public:
Matrix<int> m1; //将对象引入间接确定正确的指针位置
MatrixIterator(T* im, Matrix<T>& mm) {
this->m_T = im;
this->m1 = mm;
bool operator != (MatrixIterator& it)
return this->m_T != it.m_T;
void operator ++(int)
m_T += m1.m_y; //关键以抽象矩阵行首地址为迭代目标
T operator * ()
return *m_T;
int Get(int i) { //相当于operator []
return m_T[i];
template<class T>
void display(T strat, T end, Matrix<int>& m)
cout << endl;
for (T mid = strat; mid != end; mid++) {
for (int i = 0; i < m.m_y; i++)
cout << mid.Get(i) << "\t";
cout << endl;
int main()
Matrix<int> ary(7, 7);
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
ary.insertValue(i, j, i);
MatrixIterator<int>start(ary.Begin(),ary);
MatrixIterator<int>end(ary.End(),ary);
cout << "数组元素为:";
display(start, end, ary);
return 0;
}
结果展示:
想法拓展:
使用二维数组实现矩阵迭代怎么实现呢?
话不多说代码展示:
matrix类实现:
#ifndef MATRIX_CPP_
#define MATRIX_CPP_
#include<iostream>
using namespace std;
class Matrix {
int m_Row; // 矩阵的行数
int m_Col; // 矩阵的列数
public:
double** m_pMatrix; // 通过二重指针来实现动态矩阵
Matrix() {
Matrix operator = (Matrix rightM) //在MatrixIterator类中需要进行Matrix的赋值
if (this != &rightM)
this->m_Col = rightM.m_Col;
this->m_Row = rightM.m_Row;
this->m_pMatrix = rightM.m_pMatrix;
//rightM.m_pMatrix = nullptr; //不能将原指针NULL,本函数作用只是赋值
return *this;
Matrix(int m, int n)
m_Row = m;
m_Col = n;
if (m < 0 || n < 0)
return;
alloc(m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
m_pMatrix[i][j] = j;
void show()
cout << "矩阵行数为:" << m_Row << endl;
cout << "矩阵列数为:" << m_Col << endl;
cout << "矩阵各元素为:" << endl;
for (int i = 0; i < m_Row; i++)
for (int j = 0; j < m_Col; j++)
if (j == 0)
cout << " | ";
cout << m_pMatrix[i][j] << " ";
cout << "|";
cout << endl;
}; // 打印出矩阵的行数、列数及各元素的值
double* Begin() { //返回行的首地址
return m_pMatrix[0];
double* end() { //返回行的尾地址
return m_pMatrix[m_Row - 1];
double* Getpos(double* old) { //实现指针的不断更新
for (int i = 0; i < m_Row - 1; i++) {
if (m_pMatrix[i] == old) {
return m_pMatrix[i + 1];
} //matrixiterator中值为一维指针,简单的指针赋值无法将二维数组正确表示,
//这里通过旧一维地址在二维地址中确认间接得出正确的行首地址
int getCol() { //方便display中遍历列值
return this->m_Col;
inline void alloc(int rowNum, int colNum)//调用动态内存分配函数
m_pMatrix = new double* [rowNum];
for (int i = 0; i < rowNum; i++)
m_pMatrix[i] = new double[colNum];
}//实现动态内存分配,将此部分独立出来,防止代码重复出现过于累赘
#endif;
matrixiterator类实现:
#ifndef MATRIXITERATOR_CPP_
#define MATRIXITERATOR_CPP_
#include "matrix.cpp"
#include<iostream>
class MatrixIterator {
double* m; //行的首地址
public:
Matrix m1;//解决行首地址位置问题,将二维数组引入,public 可在display中访问getCol函数
MatrixIterator(double* im, Matrix& mm) {
this->m = im;
this->m1 = mm;
bool operator !=(MatrixIterator& im) {
return this->m != im.m;
void operator ++(int) {
m = m1.Getpos(m);
double operator* () {
return *m;
double Get(int i) { //相当于operator []
return m[i];
#endif
主函数实现:
#include<iostream>
#include"matrixiterator.cpp"
#include"matrix.cpp"
using namespace std;
void display(MatrixIterator start, MatrixIterator end) {
for (MatrixIterator mid = start; mid != end; mid++ ){
for(int i = 0; i < start.m1.getCol(); i ++)
cout<< mid.Get(i) << " ";
cout << endl;
int main() {
Matrix mm(5, 8);
MatrixIterator start(mm.Begin(),mm);
MatrixIterator end(mm.end(),mm);