[Solved └(°ᴥ°)┘ ] - QVector VS Array
-
Hi,
I would like to have a standard array as an attribute to a class
I know the size of the array will be fixed and known in advance, so I thought array would be more appropriate that a QVector in my case.
However, how is it possible to declare a standard array in the header and initialize it in the constructor ? Is it even possible? The size is known but I cannot specify it in my header, only in the constructor.. (i.e: the size will vary from object to object)@double arrayMeanCadence1sec[] // do not compile@
I tried with a QVector but when I try to insert element like a standard array, it doesn't insert, example:
@QVector<double> vecMeanCadence1sec;
vecMeanCadence1sec.insert(index, value)
/// size() of vecMeanCadence1sec is still at 0 !@I could only get QVector to work with .append(), but this is not what I want, the index has a meaning, @arrayMeanCadence1sec[5]@ means the average at 5 second, if it's inserted in a wrong index, the data will be corrupted.
What would be a good data structure for this, Is there a way to make a standard C++ array works?
Thanks ! -
Oops I forgot to erase a line that was doing .append()
With the standard array syntax, the QVector doesn't get incremented
See example :
@ qDebug() << "OK INSERTING AVG NOW" << avg1SecCad << "at index:" << currentSec << "vectorSize now" << vecMeanCadence1sec.size();
vecMeanCadence1sec[currentSec] = avg1SecCad;
qDebug() << "size QVector now :" << vecMeanCadence1sec.size();
// vecMeanCadence1sec.append(avg1SecCad);
@QDebug Log :
OK INSERTING AVG NOW 150.333 at index: 1 vectorSize now 0
size QVector now : 0
OK INSERTING AVG NOW 147.5 at index: 2 vectorSize now 0
size QVector now : 0--
All I did on the QVector is declare it as a private member of the class:
@QVector<double> vecMeanCadence1sec;@
and then I did this in the constructor :
@ /// Instantiate array with good size so it doesn't get copied
vecMeanCadence1sec.reserve(workout.getDurationMinutes()*60 + 100);
vecMeanCadence1sec.fill(-1.0);@ -
Hi,
You should rather use resize, reserve is just meant for tuning QVector memory usage.
-
Since you are instantiating the vector in your constructor and not resizing it anywhere else, there's no need for that.
You can even just use one of the overloaded constructor that takes a size parameter
-
Thanks guy,
You just saved me a couple of line of code and it's clean now.
@ const int sizeArray = workout.getDurationMinutes()*60 + 5;
const double initValue = -1.0;vecMeanCadence1sec = QVector<double>(sizeArray, initValue); vecMeanHr1sec = QVector<double>(sizeArray, initValue); vecMeanPower1sec = QVector<double>(sizeArray, initValue); vecMeanSpeed1sec = QVector<double>(sizeArray, initValue);@
About time you set a dogecoin wallet so we can give donation every time you help ! :)
http://dogecoin.com/
http://www.reddit.com/r/dogecoin