Best implementation of a vector of vectors in C++ [closed]

In general, if you got a large number of elements, it is not a good idea to nest two STL classes, because STL containers have a certain overhead as compared to just the stored element sizes. If you nest two STL containers, then this overhead gets multiplied by the number of elements of the “outer” container.

If you need a 2D array with rows that have different lengths, then you can use the “compressed row storage” format [1], it is widely used in numerical algorithms, to represent sparse matrices.

References:
[1] https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_row_Storage_.28CRS_or_CSR.29

Leave a Comment