You can do it with nested vectors:
int number_of_rows, number_of_columns;
std::vector < std::vector <int> > array;
array.resize(number_of_rows);
for (int i=0; i < number_of_rows; ++i)
array[i].resize(number_of_columns);
array[1][2] = 3; // e.g. of assignment
Initializing the complete array size in the constructor:
std::vector <std::vector <int> > (nor, std::vector<int>(noc));
Note however that a nested std::vector
structure does not bind you to a two dimensional array -- every one of the nested vectors can have a different size. E.g.:
std::vector <std::vector <int> > not_array(3);
// contains 3 inner empty vectors
for (int i=0; i < 3; ++i)
arrray[i].resize(i+1);
Now the not_array
contains vectors of sizes 1, 2 and 3 in that order.
If you want to be sure you are using a matrix (2D), not just a vector of (1D) vectors, you can use an implementation from an outside library.
OpenCV
is a computer vision library, and contains class cv::Mat
boost
is a general-purpose library and has an implementation of matrix
class