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?

QVector has no assign method?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 6 Posters 1.2k 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.
  • JonBJ JonB

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

    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on 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

    JonBJ 1 Reply Last reply
    0
    • JonBJ JonB

      @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 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
      • jsulmJ jsulm

        @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 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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • P Publicnamer

          @jsulm said in QVector has no assign method?:

          Such a thing would be extremely dangerous

          Not necessarily. It depends on the use case.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          • jsulmJ jsulm

            @JonB said in QVector has no assign method?:

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

            Why not?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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?

            VRoninV 1 Reply Last reply
            0
            • JonBJ JonB

              @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?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on 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

              JonBJ 1 Reply Last reply
              2
              • VRoninV VRonin

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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?

                VRoninV 1 Reply Last reply
                0
                • JonBJ JonB

                  @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?

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  2
                  • VRoninV VRonin

                    @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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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

                      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.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 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

                      JonBJ 1 Reply Last reply
                      0
                      • JKSHJ JKSH

                        @Publicnamer said in QVector has no assign method?:

                        And yet a hash isn't good enough.

                        Why? Can you describe your use-case?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #17

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

                        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