Results 1 to 3 of 3

Thread: QList or QVector to represent bidimensional arrays? Which one is better?

  1. #1
    Join Date
    Mar 2015

    Default QList or QVector to represent bidimensional arrays? Which one is better?


    I want to create a bidimensional array(int matrix[5][5]) using QT libraries but i don't know which "technique" to choose.
    Which one do you think is more correct?
    Qt Code:
    1. QList<QVector<int>> matrix;
    2. //or
    3. QVector<QVector<int>> matrix;
    To copy to clipboard, switch view to plain text mode 

    In my case, i cannot use "int matrix[5][5]" directly because i'll read the data from a file and put the data(numbers) in a bidimensional array. I could use directly, but i would have to read the file two times to do that. First, to discover how many line and columns he has, then i could create the array, like "int matrix[lines][cols]" and the second time to put the data in it. I think is better using QList/QVector.


  2. #2
    Join Date
    Dec 2009
    New Orleans, Louisiana
    Thanked 153 Times in 150 Posts
    Qt products
    MacOS X

    Default Re: QList or QVector to represent bidimensional arrays? Which one is better?

    QList is a good general choice. QVector might be better for your use if you know the size of the two dimensional array in advance, but it sounds like you don't know in advance how many rows/columns you may have.

    QVector stores items adjacent in memory, so expanding a QVector is more expensive in the sense that a larger memory allocation is made and old items copied, etc. If you have a reasonable guestimate regarding size, you can pre-allocate a QVector with QVector::resize() or by passing the size in the constructor, i.e. QVector<int> vec(200).

    Otherwise, QList is probably a better choice for your purpose.

    Hope that helps.

  3. The following user says thank you to jefftee for this useful post:

    robgeek (24th March 2015)

  4. #3
    Join Date
    Oct 2009
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: QList or QVector to represent bidimensional arrays? Which one is better?

    You could represent the matrix as a flat array, for instance in a QVector<int>. For instance, if the format of the file is:

    <element at row 0, column 0>, ..., <element at row 0, column (width - 1)>
    <element at row (height - 1), column 0>, ..., <element at row (height - 1), column (width - 1)>

    then you could parse the whole file in one go, doing these things:
    • append each element to the QVector v;
    • while parsing a row, keep track of the current column;
    • upon finishing the first row, store the width in a variable;
    • upon finishing each subsequent row, check that it has the same width as the first one.

    When you are done, the element at row r and column c is simply v[r * width + c]. You can hide this computation in a simple class that keeps the QVector as a private member. Such a flat representation has better cache locality than, say, a QVector<QVector<int>> which adds at least one level of indirection.

Similar Threads

  1. 2D array in Qt: QVector or QList
    By timmu in forum Newbie
    Replies: 8
    Last Post: 26th April 2017, 18:08
  2. Replies: 5
    Last Post: 3rd September 2011, 00:11
  3. Represent a bit vector
    By danilodsp in forum Newbie
    Replies: 3
    Last Post: 20th October 2010, 17:10
  4. Replies: 4
    Last Post: 20th August 2010, 14:54
  5. QQueue:QList::QStack:QVector?
    By jamadagni in forum Qt Programming
    Replies: 2
    Last Post: 20th August 2007, 13:29


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.