I am creating a class that needs to store different arrays like data. Those arrays will have mutable size, but all of the arrays inside the class will have the same size. The arrays will be later used for number crunching in methods provided by the class.
What is the best/standard way of declaring that kind of data inside the class?
Solution 1 – Raw arrays
class Example {
double *Array_1;
double *Array_2;
double *Array_3;
int size; //used to store size of all arrays
};
Solution 2 – std::vector for each array
class Example {
vector<double> Array_1;
vector<double> Array_2;
vector<double> Array_3;
};
Solution 3 – A struct that stores each vertex and have a std::vector of that struct
struct Vertex{
double Var_1;
double Var_2;
double Var_3;
};
class Example {
vector<Vertex> data;
};
My conclusion as a beginner would be:
Solution 1 would have the best performance but would be the hardest to implement.
Solution 3 would be elegant and easier to implement, but I would run into problems when performing some calculations because the information would not be in an array format. This means numeric regular functions that receive arrays/vectors would not work (I would need to create temporary vectors in order to do the number crunching).
Solution 2 might be the middle of the way.
Any ideas for a 4th solution would be greatly appreciated.
vectorpretty much gets optimized away.Vertexstructure. And using a vector (or array) of vertex structures is very common and often used in "mathematical calculations" using parallelism or CUDA kernels or the like.std::arrayorstd::vector. Not raw arrays.arrayis obviously wrong here—this is betweenunique_ptrandvector, and is really the long-rejecteddynarray(plus thestructbusiness).