I know vectors can be constructed to a predefined size
vector<int> foo(4);
But is there a way to specify the dimensions of nested vectors?
vector< vector<int> > bar(4);
Lets say I wanted a vector of size 4 containing vector's of size 4... like a 4x4 multidimensional array of ints?
The second argument to that constructor is the value to initialize with. Right now you're getting 4 default-constructed vectors. To clarify with a simpler 1D example:
So you want:
This creates a vector of vectors of ints, initialized to contain 4 vectors that are initialized to contain 4 ints, initialized to 0. (You could specify a default value for the int to, if desired.)
A mouth-full, but not too hard. :)
For a pair:
Alternatively to a
std::vector
you can useboost::multi_array
. From the documentation: