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. interesting problem with QVector
Forum Updated to NodeBB v4.3 + New Features

interesting problem with QVector

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.4k 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    I'm trying to populate a 2D QVector in my app. I'd assumed that QVectors would behave as do STL vectors, but I discovered something odd.

    This builds and runs fine:

        vector <vector <int> > v;
        v.resize(3);
        v.at(0).push_back(55);
    

    But this does not:

        QVector <QVector <int> > qv;
        qv.resize(3);
        qv.at(0).push_back(55);
    

    I get a compile-time error:
    passing 'const QVector<int>' as 'this' argument discards qualifiers [-fpermissive]

    Am I doing something wrong, or is this a limitation of QVectors? Interestingly enough, if I use brackets instead of the ".at()" it also works.

    JonBJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

      I'm trying to populate a 2D QVector in my app. I'd assumed that QVectors would behave as do STL vectors, but I discovered something odd.

      This builds and runs fine:

          vector <vector <int> > v;
          v.resize(3);
          v.at(0).push_back(55);
      

      But this does not:

          QVector <QVector <int> > qv;
          qv.resize(3);
          qv.at(0).push_back(55);
      

      I get a compile-time error:
      passing 'const QVector<int>' as 'this' argument discards qualifiers [-fpermissive]

      Am I doing something wrong, or is this a limitation of QVectors? Interestingly enough, if I use brackets instead of the ".at()" it also works.

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

      @mzimmers

      http://doc.qt.io/qt-5/qvector.html :

      const T &QVector::at(int i) const
      
      T &QVector::operator[](int i)
      

      Correct me if I'm wrong, but doesn't at() deliver read-only?

      mzimmersM 1 Reply Last reply
      6
      • JonBJ JonB

        @mzimmers

        http://doc.qt.io/qt-5/qvector.html :

        const T &QVector::at(int i) const
        
        T &QVector::operator[](int i)
        

        Correct me if I'm wrong, but doesn't at() deliver read-only?

        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by mzimmers
        #3

        @JNBarchan hmm, yes, you're absolutely right. That explains the compiler error.

        So, I guess I have to use the brackets? Not a problem, though I think that syntax is discouraged by the C++ purists (when using the STL).

        T &QVector::operator[](int i)
        Returns the item at index position i as a modifiable reference. (emphasis mine)

        JonBJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @JNBarchan hmm, yes, you're absolutely right. That explains the compiler error.

          So, I guess I have to use the brackets? Not a problem, though I think that syntax is discouraged by the C++ purists (when using the STL).

          T &QVector::operator[](int i)
          Returns the item at index position i as a modifiable reference. (emphasis mine)

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

          @mzimmers

          I'm guessing push_back() wants to modify the thing?

          And what's the declaration of the STL vector::at()?

          http://doc.qt.io/qt-5/qvector.html#details :

          QVector uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator. On non-const vectors, operator returns a reference to the item that can be used on the left side of an assignment:

          if (vector[0] == "Liz")
              vector[0] = "Elizabeth";
          

          For read-only access, an alternative syntax is to use at():

          for (int i = 0; i < vector.size(); ++i) {
              if (vector.at(i) == "Alfonso")
                  cout << "Found Alfonso at position " << i << endl;
          }
          

          at() can be faster than operator, because it never causes a deep copy to occur.

          1 Reply Last reply
          3

          • Login

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