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. QVector has no assign method?
Forum Updated to NodeBB v4.3 + New Features

QVector has no assign method?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 6 Posters 1.2k Views 1 Watching
  • 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.
  • P Publicnamer
    22 Nov 2021, 13:52

    Does QVector not have the ability to set the value at any index e.g.

     int index = rand();
     myvector[index] = rand();
    

    There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.

    BTW I see that Qt5 has no QArray. Not sure if there formally was one.

    J Offline
    J Offline
    JonB
    wrote on 22 Nov 2021, 14:15 last edited by JonB
    #3

    @Publicnamer said in QVector has no assign method?:

    And yet a hash isn't good enough.

    I don't understand this statement.

    If you want no array/vector resizing, and no pre-allocated maximum elements, and you don't want to use a list (do you?), it's a QHash or a QMap.

    1 Reply Last reply
    3
    • J jsulm
      22 Nov 2021, 13:55

      @Publicnamer said in QVector has no assign method?:

      There are many cases where it's important to not bother with resizing

      With QVector you have to. index must be: 0 <= index < myvector.size()
      https://doc.qt.io/qt-5/qvector.html#operator-5b-5d
      QVector is basically a resizeable array.

      P Offline
      P Offline
      Publicnamer
      wrote on 22 Nov 2021, 15:04 last edited by
      #4

      @jsulm said in QVector has no assign method?:

      QVector is basically a resizeable array.

      Isn't there an automatically resized array?

      J J 2 Replies Last reply 22 Nov 2021, 15:07
      0
      • P Publicnamer
        22 Nov 2021, 15:04

        @jsulm said in QVector has no assign method?:

        QVector is basically a resizeable array.

        Isn't there an automatically resized array?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 22 Nov 2021, 15:07 last edited by
        #5

        @Publicnamer said in QVector has no assign method?:

        Isn't there an automatically resized array?

        Such a thing would be extremely dangerous as it would simply resize if the programmer uses an invalid index instead of crashing the application with "out of bounds" exception.
        Why do you need such a thing actually?! What is the problem to properly resize the QVector?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply 22 Nov 2021, 16:05
        2
        • P Publicnamer
          22 Nov 2021, 15:04

          @jsulm said in QVector has no assign method?:

          QVector is basically a resizeable array.

          Isn't there an automatically resized array?

          J Offline
          J Offline
          JonB
          wrote on 22 Nov 2021, 15:34 last edited by JonB
          #6

          @Publicnamer said in QVector has no assign method?:

          Isn't there an automatically resized array?

          As @jsulm says.
          However if you really want this then (presumably, untested) you can derive from QVector and override the [] operator?

          EDIT
          I don't think you can override the [] operator after all?
          In which case it may not be as "elegant" as you might wish, but you do realize that before going [index] you can insert

          if (index >= qVector.size())
              qVector.resize(index);
          

          Up to you whether you want to do this where you are using qVector[index] only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology).

          J P 2 Replies Last reply 22 Nov 2021, 16:00
          2
          • J JonB
            22 Nov 2021, 15:34

            @Publicnamer said in QVector has no assign method?:

            Isn't there an automatically resized array?

            As @jsulm says.
            However if you really want this then (presumably, untested) you can derive from QVector and override the [] operator?

            EDIT
            I don't think you can override the [] operator after all?
            In which case it may not be as "elegant" as you might wish, but you do realize that before going [index] you can insert

            if (index >= qVector.size())
                qVector.resize(index);
            

            Up to you whether you want to do this where you are using qVector[index] only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology).

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 22 Nov 2021, 16:00 last edited by
            #7

            @JonB said in QVector has no assign method?:

            I don't think you can override the [] operator after all?

            Why not?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 1 Reply Last reply 22 Nov 2021, 16:17
            0
            • J JonB
              22 Nov 2021, 15:34

              @Publicnamer said in QVector has no assign method?:

              Isn't there an automatically resized array?

              As @jsulm says.
              However if you really want this then (presumably, untested) you can derive from QVector and override the [] operator?

              EDIT
              I don't think you can override the [] operator after all?
              In which case it may not be as "elegant" as you might wish, but you do realize that before going [index] you can insert

              if (index >= qVector.size())
                  qVector.resize(index);
              

              Up to you whether you want to do this where you are using qVector[index] only as an l-value or also as an r-value (i.e. when writing to it versus reading from it, if I am using the wrong terminology).

              P Offline
              P Offline
              Publicnamer
              wrote on 22 Nov 2021, 16:04 last edited by
              #8

              @JonB said in QVector has no assign method?:

              if (index >= qVector.size())
              qVector.resize(index);

              Yes that's what I'm doing now. It's a tiny inconvenience.

              1 Reply Last reply
              0
              • J jsulm
                22 Nov 2021, 15:07

                @Publicnamer said in QVector has no assign method?:

                Isn't there an automatically resized array?

                Such a thing would be extremely dangerous as it would simply resize if the programmer uses an invalid index instead of crashing the application with "out of bounds" exception.
                Why do you need such a thing actually?! What is the problem to properly resize the QVector?

                P Offline
                P Offline
                Publicnamer
                wrote on 22 Nov 2021, 16:05 last edited by
                #9

                @jsulm said in QVector has no assign method?:

                Such a thing would be extremely dangerous

                Not necessarily. It depends on the use case.

                C 1 Reply Last reply 22 Nov 2021, 16:06
                0
                • P Publicnamer
                  22 Nov 2021, 16:05

                  @jsulm said in QVector has no assign method?:

                  Such a thing would be extremely dangerous

                  Not necessarily. It depends on the use case.

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 22 Nov 2021, 16:06 last edited by
                  #10

                  @Publicnamer said in QVector has no assign method?:

                  Not necessarily. It depends on the use case.

                  Your example above will result in an oom exception after a few milliseconds. If you don't call this dangerous...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • J jsulm
                    22 Nov 2021, 16:00

                    @JonB said in QVector has no assign method?:

                    I don't think you can override the [] operator after all?

                    Why not?

                    J Offline
                    J Offline
                    JonB
                    wrote on 22 Nov 2021, 16:17 last edited by
                    #11

                    @jsulm said in QVector has no assign method?:

                    I don't think you can override the [] operator after all?

                    Because operator[] is listed among Public functions but does not say virtual?

                    V 1 Reply Last reply 22 Nov 2021, 17:49
                    0
                    • J JonB
                      22 Nov 2021, 16:17

                      @jsulm said in QVector has no assign method?:

                      I don't think you can override the [] operator after all?

                      Because operator[] is listed among Public functions but does not say virtual?

                      V Offline
                      V Offline
                      VRonin
                      wrote on 22 Nov 2021, 17:49 last edited by VRonin
                      #12

                      @JonB said in QVector has no assign method?:

                      Because operator[] is listed among Public functions but does not say virtual?

                      unlikely you need operator[] to be virtual. You normally don't use base classes interfaces for containers.

                      #include<QVector>
                      template <class T>
                      class ExpandingVector : public QVector<T>{
                      public:
                          using QVector<T>::QVector;
                          T& operator[](int index) {
                              if (index >= QVector<T>::size())
                                  QVector<T>::resize(index+1);
                              return QVector<T>::operator[](index);
                          }
                      };
                      

                      P.S.

                      There are many cases where

                      I still think there is no case where this is useful but you do you bro.

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      J 1 Reply Last reply 22 Nov 2021, 17:55
                      2
                      • V VRonin
                        22 Nov 2021, 17:49

                        @JonB said in QVector has no assign method?:

                        Because operator[] is listed among Public functions but does not say virtual?

                        unlikely you need operator[] to be virtual. You normally don't use base classes interfaces for containers.

                        #include<QVector>
                        template <class T>
                        class ExpandingVector : public QVector<T>{
                        public:
                            using QVector<T>::QVector;
                            T& operator[](int index) {
                                if (index >= QVector<T>::size())
                                    QVector<T>::resize(index+1);
                                return QVector<T>::operator[](index);
                            }
                        };
                        

                        P.S.

                        There are many cases where

                        I still think there is no case where this is useful but you do you bro.

                        J Offline
                        J Offline
                        JonB
                        wrote on 22 Nov 2021, 17:55 last edited by
                        #13

                        @VRonin
                        This is very useful, but I don't understand :)
                        If I use your template, but the base opertaor[] isn't virtual/overridable, what happens when some other code receiving my array as QVector<> parameter, not ExpandingVector<>, and then accesses it via []? They won't get my oevrride if it wasn't virtual?

                        V 1 Reply Last reply 22 Nov 2021, 18:02
                        0
                        • J JonB
                          22 Nov 2021, 17:55

                          @VRonin
                          This is very useful, but I don't understand :)
                          If I use your template, but the base opertaor[] isn't virtual/overridable, what happens when some other code receiving my array as QVector<> parameter, not ExpandingVector<>, and then accesses it via []? They won't get my oevrride if it wasn't virtual?

                          V Offline
                          V Offline
                          VRonin
                          wrote on 22 Nov 2021, 18:02 last edited by
                          #14

                          @JonB That's correct but 3rd party code receiving a QVector<> would need to stick to the QVector contract (i.e. index must be within bounds). ExpandingVector will behave exactly like QVector in that case.

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          J 1 Reply Last reply 22 Nov 2021, 18:12
                          2
                          • V VRonin
                            22 Nov 2021, 18:02

                            @JonB That's correct but 3rd party code receiving a QVector<> would need to stick to the QVector contract (i.e. index must be within bounds). ExpandingVector will behave exactly like QVector in that case.

                            J Offline
                            J Offline
                            JonB
                            wrote on 22 Nov 2021, 18:12 last edited by
                            #15

                            @VRonin
                            Then I understand completely, thank you.

                            You just have your own definition of operator[]. You might as well have defined it as a foobar() method :)

                            1 Reply Last reply
                            1
                            • P Publicnamer
                              22 Nov 2021, 13:52

                              Does QVector not have the ability to set the value at any index e.g.

                               int index = rand();
                               myvector[index] = rand();
                              

                              There are many cases where appending is not helpful, but assignment to a "random" index is, and yet it's important to not bother with resizing, and where some entries may have a default value e.g. an array of pointers where the default is NULL. And yet a hash isn't good enough.

                              BTW I see that Qt5 has no QArray. Not sure if there formally was one.

                              J Offline
                              J Offline
                              JKSH
                              Moderators
                              wrote on 23 Nov 2021, 01:57 last edited by
                              #16

                              @Publicnamer said in QVector has no assign method?:

                              And yet a hash isn't good enough.

                              Why? Can you describe your use-case?

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              J 1 Reply Last reply 23 Nov 2021, 06:56
                              0
                              • J JKSH
                                23 Nov 2021, 01:57

                                @Publicnamer said in QVector has no assign method?:

                                And yet a hash isn't good enough.

                                Why? Can you describe your use-case?

                                J Offline
                                J Offline
                                JonB
                                wrote on 23 Nov 2021, 06:56 last edited by JonB
                                #17

                                @JKSH I asked this too at the start, but no response....

                                1 Reply Last reply
                                0

                                12/17

                                22 Nov 2021, 17:49

                                • Login

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