没有合适的资源?快使用搜索试试~ 我知道了~
首页C++实现一个二维数组类
#ifndef __H_MATRIX_H__ #define __H_MATRIX_H__ #include <vector> using namespace std; template<typename> class matrix { public: matrix(int rows, int cols):array(rows) { for (int i=0; i<rows> & operator[](int row)const { return a
资源详情
资源评论
资源推荐

C++实现一个二维数组类实现一个二维数组类
#ifndef __H_MATRIX_H__
#define __H_MATRIX_H__
#include <vector>
using namespace std;
template<typename Object>
class matrix
{
public:
matrix(int rows, int cols):array(rows)
{
for (int i=0; i<rows; i++)
array[i].resize(cols);
}
const vector<Object> & operator[](int row)const
{
return array[row];
}
vector<Object>& operator[](int row)
{
return array[row];
}
int numrows()const
{
return array.size();
}
int numcols()const
{
return numrows() ? array[0].size() : 0;
}
private:
vector< vector<Object> > array;
};
#endif//__H_MATRIX_H__
test.cpp
#include "matrix.h"
#include <iostream>
using namespace std;
int main()
{
matrix<int> matrixArray(3,5);
int i,j;
for (i=0; i<matrixArray.numrows(); i++)
{
for(j=0; j<matrixArray.numcols(); j++)
matrixArray[i][j] = i+j;
}
for (i=0; i<matrixArray.numrows(); i++)
{
for(j=0; j<matrixArray.numcols(); j++)
cout<< matrixArray[i][j]<<ends;
cout<<endl;
}
return 0;
}















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0