#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;
}