vector<vector<double>> vs c array

Dear friends:
The function "DBPutQuadvar1" in silo library need input into the following two dimensional array "comp" which in defined as following in the tutorial:
1
2
3
     double *comp[2];  
     comp[0] = (double *)malloc(sizeof(double)*Grid.NX*Grid.NY);
     comp[1] = (double *)malloc(sizeof(double)*Grid.NX*Grid.NY);


But I want to input the 2D array using c++ vector which in defined as follows:
1
2
3
     vector<vector<double>>   uv_var(2);    
     uv_var[0].resize(Grid.NX*Grid.NY);
     uv_var[1].resize(Grid.NX*Grid.NY);


But it output different results. Does the vector<vector<double>> works in the same way with the 2D c array?
Could you please give me some suggestions.
Regards
Last edited on
> vector<vector<double>> works in the same way with the 2D c array?

No. An MxN 2D c array is an array of arrays; it contains M*N contiguous elements

What we require here - double *comp[2]; - is a 1D array containing two pointers:

We can create it this way:
1
2
3
4
5
6
// create the vector: uv_var consists of 2 vectors, each vector is of size Grid.NX*Grid.NY  
vector<vector<double>>   uv_var( 2, vector<double>(Grid.NX*Grid.NY) );    

// create a c-style array containing two pointers (pointing to the first items in the vectors uv_var[0] and uv_var[1]) 
double* comp[2] = { std::addressof( uv_var[0].front() ) , std::addressof( uv_var[1].front() ) }; 
// pass comp to the library 
Thank you very much for your help.
Topic archived. No new replies allowed.