After some extensive search I could not find a clear answer to the following issue. When one is to compare the 1D (vector<type>) vs 2D (<vector<vector<type>>>) vector implementation there is obviously a matter of execution time etc. These issues were discussed here: 2D vector vs 1D vector.
However, consider for example matrix multiplication function. If two, for example, 2x2 and 2x2 matrices are multiplied, a 2x2 matrix results. However, one can use also 1D matrices, for example like here (I try to use a clumsy yet mathematical notation; consider all dimensions appropriately matching):
[ 2D matrix, K rows x N columns ] * [ 1D column vector, N elements ] = [ 1D column vector, K elements ]
Basically, 2D vector is multiplied by 1D vector, and results in 1D vector. Another possibility is to use 2D dimensional vector, like this:
[ 2D matrix, K rows x N columns ] * [ 2D matrix, N rows x 1 column ] = [ 2D matrix, K rows x 1 column ]
So only 2D vectors are used. Finally, the implementation may work like this:
[ 2D matrix, K rows x N columns ] * [ 2D matrix, N rows x 1 column ] = [ 1D column vector, K elements ]
where despite input values are 2D vectors, the result is recognized as 1D and as such provided by the function.
The particular implementation is not that important. One can write a function that takes any type of the vector (1D or 2D). Overall, the result is what matters, if such function is to be used frequently in the program. Basically, the questions are what is the most common practise? What is in principle better to use in the program? 2D vectors are more powerful, yet slower. Also, what bothers me, and may make my code chaotic is the decision whether to use 1D vectors
vector<type> vec(N, def_val);
whenever possible, or 2D vectors, which (in case of a 1 dimension) would have, in fact, two possibilities:
vector<vector<type>> vec(N, vector<type>(1, def_val));
vector<vector<type>> vec(1, vector<type>(N, def_val));
Many thanks for comments on this topic.
std::array<std::array<type>,width>,height>and then you do all the index calculations by hand (or use std::mdspan to do that for you). Benefit you now have a "strong type" so it will be more clear what your code does.