Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved └(°ᴥ°)┘ ] - QVector VS Array
QtWS25 Last Chance

[Solved └(°ᴥ°)┘ ] - QVector VS Array

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    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 !


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      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;
      }
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

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


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

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


          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

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

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


              Free Indoor Cycling Software - https://maximumtrainer.com

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maximus
                  wrote on last edited by
                  #8

                  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


                  Free Indoor Cycling Software - https://maximumtrainer.com

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved