How do I store a list of arrays into another set of array? I tried this way but it doesn’t work.
float data1[5] = {150.0, 203.0, 165.0, 4.0, 36.0};
float data2[5] = {249.0, 255.0, 253.0, 104.0, 2.0};
float allData[2] = {data1, data2};
cout << allData[1][2] << endl; //this should print 253.0 but it has error
This didn’t allow me to compile. I also tried to change it to float *allData[2] = {data1, data2}; and it allowed me to compile but I don’t get the result I want.
What have I done wrong in this?
Thanks.
You should use vectors (this example is in C++11):
Note: possibly you want to store pointers to vectors in allData to prevent copying the data, but you should always take care with such constructs as this could very quickly lead to dangling pointers. This is also the case for the solution with plain arrays by the way.
Edit, as R. Martinho Fernandes mentioned in comments:
You can change the construct of
allDatato:It’s worth to note however that after this operation
data1anddata2will be emtpy as their contents are moved toallData. If you do not need them anymore this is the version to prefer (no pointers, no copying).