Unlike <vector> where you don't really have restrictions on memory allocation in the singular direction, you can't assume this to be the case with matrices - namely you have to set the extent in at least one direction. |
No. That's an incorrect statement. Are you thinking of arrays in C?
First of all, there is no std::matrix in C++, so what you wrote is a "vector of vectors"--perfectly acceptable C++ code. Now, if you as the programmer decide that one or the other (or both) dimension of this construct must be fixed, that's perfectly fine, and an initial
resize
operation is totally appropriate. But it doesn't have to be that way.
There is nothing wrong with starting with an empty outer vector. As new rows (inner vectors) are added, the outer vector can be resized. Reserving room for the expected maximum outer vector size is a good idea, but if M[5] is the last row that is added, there is no need to create M[6] - M[11]. You can if you want to, but you don't need to.
You could also make the inner vectors (rows) a fixed size when they are created, or reserve space for each of them if you want to. That's a design/implementation decision based on the needs of the program.
If "you have to set the extent in at least one direction" is inherently part of the problem you are trying to solve, spell it out for us. But from a language perspective, it is not true.