Hello!

The program reads data from a file containing 7440 lines. Each line is written to the vector.

How can I transfer elements of this vector to a four-dimensional array?

void myClass::conversion() { int ix, iy,iz; double* myarray=&myvec[0]; typedef double MyType[26][2][40][5]; for(int i=0 ;i<26;++i) { for(int j=0; j<2; ++j) { for(int k=0;k<40; ++k) { if(!index(i, j ,k, iz, ix, iy))continue; //проверяем индексы for(int l=0; l<5; ++l) { cout<<reinterpret_cast<MyType &>(*myarray)[0][j][k][l]<<endl; } } } } } 

Printing produces completely different values, they are present in the file, but the indexing does not match, although a similar test for a two-dimensional array worked. In addition to reinterpret_cast , what other solutions are there for such problems?

  • Not obvious what is myvec - vector<double> ? and it is not known whether you are correctly recording the data read from the file - maybe you are confusing something there in the indexes ... - Harry
  • @Harry The type of the vector is also double , the filling is performed correctly, because the value for the index n corresponds to the value from the file for the line n + 1 - DefileR

1 answer 1

Maybe it's just a matter of

 [0][j][k][l] 

instead

 [i][j][k][l] 

?

  • Good point, but for some reason, if you replace [0] with [i] , then the print gives out any garbage in the form of zeros and negative numbers. - DefileR
  • Then give the code how you write the data. The output on your principle is quite workable (see, for example, ideone.com/I5rrWk ). Best of all - give a minimal self-sufficient reproducible example ru.stackoverflow.com/help/mcve - Harry