Results 1 to 9 of 9

Thread: 2D array in Qt: QVector or QList

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default 2D array in Qt: QVector or QList

    Hi,

    I need to store strings in a 2D array (table). Example:

    car train bike
    boat car scooter
    bus ship shuttle

    What is the best way to do it (I don't use GUI, it's a console application)? QVector or QList?
    Does anyone have instructions for declaring and constructing a 2D array of strings? The contents (but not the size) of the 2D array changes a lot during the runtime.

    Is this an acceptable way:
    Qt Code:
    1. QVector<QString> *array[3];
    2. for(i=0; i<3; i++) {array[i] = new QVector<QString>(3);}
    To copy to clipboard, switch view to plain text mode 

    But then how do I fill this with data. This did not work:

    Qt Code:
    1. array[0][0]="hello";
    To copy to clipboard, switch view to plain text mode 

    Thanks!
    Last edited by timmu; 18th May 2012 at 11:47.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: 2D array in Qt: QVector or QList

    Have you tried QVector<QVector<QString> >?

  3. #3
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 2D array in Qt: QVector or QList

    Thanks. Indeed I'm aware of that. However, I was unable to figure out how to use it. If someone could show how to initialize and assign 2D array this way, I'd be very thankful. I know this does not work (but why?):

    Qt Code:
    1. QVector<QVector<QString> > array[3][3];
    2. QString test="hello";
    3. array[0][0]=test;
    To copy to clipboard, switch view to plain text mode 
    Last edited by timmu; 18th May 2012 at 14:36.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: 2D array in Qt: QVector or QList

    Qt Code:
    1. QVector<QVector<QString> > vectorOfVectorsOfStrings;
    2.  
    3. for(int i = 0; i < 3; i++) //of course you might not want to init the vectors in a loop - this is just an example
    4. {
    5. QVector<QString> foo; //create a QVector of QStrings
    6. foo.push_back("fooo");
    7. foo.push_back("booo");
    8. vectorOfVectorsOfStrings.push_back(foo); //add the created vector as a line in your 2D vector
    9. }
    10.  
    11. for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++)
    12. {
    13. for(int j = 0; j < vectorOfVectorsOfStrings[i].size(); j++)
    14. {
    15. //do stuff with the QString vectorOfVectorsOfStrings[i][j]
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: 2D array in Qt: QVector or QList

    If the size of the array is known and fixed you might as well use a simple array.

    You cannot size the nested QVectors, i.e. to get a 3x5 array of empty QStrings, without either using a loop:
    Qt Code:
    1. // 3x5 array
    2. QVector<QVector<QString> > v1(3);
    3. for(int outer = 0; outer < v1.size(); ++outer)
    4. v1[outer].resize(5);
    5. qDebug() << v1;
    To copy to clipboard, switch view to plain text mode 
    or a construct like:
    Qt Code:
    1. // 3x5 array
    2. typedef QVector<QString> inner;
    3. typedef QVector<inner> outer;
    4. outer v2(3, inner(5));
    5. qDebug() << v2;
    To copy to clipboard, switch view to plain text mode 
    This second approach does allow initialising all the elements to the same, non default value:
    Qt Code:
    1. // 3 x 5 "?"
    2. outer v3(3, inner(5, "?"));
    3. qDebug() << v3;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2016
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Android

    Default Re: 2D array in Qt: QVector or QList

    Qt Code:
    1. QVector<QStringList> matrix{{"foo", "bar", "baz"}, {"hello", "world", "!"}};
    2.  
    3. qDebug() << "output: " << matrix[0];
    4.  
    5. //Will output : output: ("foo", "bar", "baz")
    6.  
    7. qDebug() << "output: " << matrix[0][1];
    8.  
    9. //Will output : "bar"
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2D array in Qt: QVector or QList

    QVector<QStringList> matrix{{"foo", "bar", "baz"}, {"hello", "world", "!"}};
    This is not a 2D array. It is a 1D QVector of 1D QList<QString>. It is not the same as a QVector< QVector< QString > > and there are very important differences in terms of memory usage and performance.

    From the QList documentation:

    The QList class is a template class that provides lists.

    QList<T> is one of Qt's generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.

    QList<T>, QLinkedList<T>, and QVector<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:

    QVector should be your default first choice. QVector<T> will usually give better performance than QList<T>, because QVector<T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.

    ...

    Note: QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.

    ...

    Internally, QList<T> is represented as an array of T if sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. Otherwise, QList<T> is represented as an array of T* and the items are allocated on the heap.

    The array representation allows very fast insertions and index-based access. The prepend() and append() operations are also very fast because QList preallocates memory at both ends of its internal array. (See Algorithmic Complexity for details).

    Note, however, that when the conditions specified above are not met, each append or insert of a new item requires allocating the new item on the heap, and this per item allocation will make QVector a better choice for use cases that do a lot of appending or inserting, because QVector can allocate memory for many items in a single heap allocation.

    Note that the internal array only ever gets bigger over the life of the list. It never shrinks. The internal array is deallocated by the destructor and by the assignment operator, when one list is assigned to another.
    ...
    If your 2D array is rectangular (i.e. has rows with the same number of columns for each row) then probably the best way is to allocate a 1D QVector< QString >:

    Qt Code:
    1. QVector< QString > stringMatrix;
    2. stringMatrix.resize( nRows * nCols );
    To copy to clipboard, switch view to plain text mode 

    This results in one contiguous data array and very efficient access to any member in it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Apr 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2D array in Qt: QVector or QList

    i want to creat a two-dimensional QVector of QLine which means in front of each line another line:

    for(int i=0;i<symZqaq.size();i++)
    {
    QVector<QLineF> sym;
    sym.append(L4);
    sym.append(L5);
    sym.append(L29);
    sym.append(L25);
    sym.append(L42);
    sym.append(L26);
    sym.append(L28);
    sym.append(L31);
    sym.append(L36);
    sym.append(L44);
    symZqaq.append(sym);
    }
    for(int i=0;i<symZqaq.size();i++)
    {
    for(int j=0;j<symZqaq[i].size();i++)
    {
    symZqaq.append(L9);
    symZqaq.append(L12);
    symZqaq.append(L24);
    symZqaq.append(L30);
    symZqaq.append(L27);
    symZqaq.append(L43);
    symZqaq.append(L7);
    symZqaq.append(L2);
    symZqaq.append(L6);
    symZqaq.append(L3);
    }
    }
    the problem is in last loop for erreur : no matching function for call to 'QVector<QVector<QLineF> >:ush_back(QLineF&)'
    symZqaq.push_back(L9);

    any help plz

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2D array in Qt: QVector or QList

    no matching function for call to 'QVector<QVector<QLineF> >:: push_back(QLineF&)'
    "symZqaq" is a QVector of QVectors, not a QVector of QLines. You can't push a QLine onto symZqaq, only a QVector< QLine >. You did it correctly in the first loop. Use the same method in the nested loop.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. extracting QVectors from a multidimensional QVEctor array
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2012, 08:45
  2. QVector array declear
    By zhxys in forum Newbie
    Replies: 8
    Last Post: 2nd February 2011, 10:17
  3. Replies: 1
    Last Post: 4th August 2010, 18:13
  4. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 13:31
  5. QVector crashes when array size is big
    By Sheng in forum Qt Programming
    Replies: 49
    Last Post: 27th February 2009, 23:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.