What is the equivalent matrix-like C-array of a nested std::vector (for C and C++ interop)?
For example, if one wanted to treat std::vector<std::vector<int>>
as some kind of int arr[n][m]
, where n
is the dimension of the outer vector and m
of the inner vector, then what structure would one use in C?
This is motivated by wanting to have a similar correspondence between matrices in C and C++ as for vectors in:
Based on additional information in the comments, let me suggest you do something like this instead:
When you do nested vectors, there's a lot of extra work happening. Also, with nested vectors, the data is not contiguous, making it hard to work with any C-apis. Notice with this data structure, the size is fixed at construction time and accessible. This is designed to be row contiguous, so for C interoperability you can access extra raw pointers like so:
Just note: if you pass this into a function, be sure to pass by reference:
If you don't pass by reference, it will copy everything, which is a bunch of (typically unnecessary) work.