Correct way to work with vector of arrays Correct way to work with vector of arrays arrays arrays

Correct way to work with vector of arrays


You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.

You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x:

std::vector<std::array<double, 4> >

(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)


Use:

vector<vector<float>> vecArray; //both dimensions are open!


There is no error in the following piece of code:

float arr[4];arr[0] = 6.28;arr[1] = 2.50;arr[2] = 9.73;arr[3] = 4.364;std::vector<float*> vec = std::vector<float*>();vec.push_back(arr);float* ptr = vec.front();for (int i = 0; i < 3; i++)    printf("%g\n", ptr[i]);

OUTPUT IS:

6.28

2.5

9.73

4.364

IN CONCLUSION:

std::vector<double*>

is another possibility apart from

std::vector<std::array<double, 4>>

that James McNellis suggested.