Vector series in practice c ++ -- using vector to construct two-dimensional arrays
2D arrays are sometimes used, but few use vector to construct a 2D array.
First of all, it should be clear that there is no two-dimensional array in the computer world, just a concept of the user. In fact, our so-called two-dimensional array must also be a continuous memory.
In many cases, we can use a vector to represent a regular two-dimensional array, as long as the index corresponds to it.
So, if I leave it alone, I want to put a vector in the vector?
# Include
# Include
Using namespace std; int main () {vector
> Array (3); for (int I = 0; I <3; I ++) array [I]. resize (3); for (int I = 0; I <3; I ++) for (int j = 0; j <3; j ++) array [I] [j] = (I * j); for (int I = 0; I <3; I ++) {for (int j = 0; j <3; j ++) cout <
When "column" is used again, the resize method of vector must be used. Otherwise, [] cannot be used for access.
The reserve of vector cannot be used to allocate capacity here, because the previous blog has analyzed the cause.
In fact, what I want to say is that if you want to build a so-called two-dimensional array, the most important thing is to use resize to allocate capacity.