One of the simplest ways is to use vector vectors
const int N = 10;
const int M = 10;
vector<vector<int>> matrix2d(N, vector<int>(M, 0));
matrix2d[0][0] = 42;
Of course you can use one vector and transfer it to the accessor class
vector<int> matrix(N * M, 0)
I will give a small example here for completeness.
template <typename T>
class Matrix2D {
std::vector<T> data;
unsigned int sizeX, sizeY;
public:
Matrix2D (unsigned int x, unsigned int y)
: sizeX (x), sizeY (y) {
data.resize (sizeX*sizeY);
}
T& operator()(unsigned int x, unsigned int y) {
if (x >= sizeX || y>= sizeY)
throw std::out_of_range("OOB access");
return data[sizeX*y + x];
}
};
, , . , vector<vector<int>>
, , , , .