[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 !

  • You can allocate QVector in constructor and use it as a regular array using square brackets.
    class MyClass
    QVector<double> arrayMeanCadence1sec;

    Class::Class() : arrayMeanCadence1sec(2014)
    arrayMeanCadence1sec[5] = value;

  • Okay I feel dump now, I don't know what I changed but now the size is increasing, blame the coffee not strong enough..

  • 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);

  • Lifetime Qt Champion


    You should rather use resize, reserve is just meant for tuning QVector memory usage.

  • Thanks SGaist, should I use both on my QVector to improve performance?

    @ vecMeanCadence1sec.reserve(workout.getDurationMinutes()*60 + 10);
    vecMeanCadence1sec.resize(workout.getDurationMinutes()*60 + 10);@

  • Lifetime Qt Champion

    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 ! :)

Log in to reply