Results 1 to 10 of 10

Thread: Simple question about list of one dimensional arrays.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Simple question about list of one dimensional arrays.

    Hello!

    I would like to know how can i make a list of arrays int using qt libraries. I have many arrays with 6 numbers, like, "int array[6];" and i would like to make a list of them. I tried something like the following code i've got compilation error.
    Qt Code:
    1. QList<int> matrix; //Did not work like what i want.
    2. <QList matrix[6]; //Same thing as above;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple question about list of one dimensional arrays.

    A QVector is the Qt data structure that is closest to an array. You can QVector::push_back() your 6 integers and then create a list of the individual vectors with QList<QVector<int> >, etc. Something like this:

    Qt Code:
    1. int array[6] = {0,1,2,3,4,5};
    2.  
    3. QVector<int> vec;
    4. vec.push_back(array[0]);
    5. vec.push_back(array[1]);
    6. vec.push_back(array[2]);
    7. vec.push_back(array[3]);
    8. vec.push_back(array[4]);
    9. vec.push_back(array[5]);
    10.  
    11. QList<QVector<int> > list;
    12.  
    13. list.push_back(vec);
    14.  
    15. QVector<int> v = list.at(0);
    To copy to clipboard, switch view to plain text mode 

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

    robgeek (24th March 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Simple question about list of one dimensional arrays.

    Thanks for your answer, but i don't understand why did you put the line number 15 in your example. I think all i need goes to the line number 13.

  5. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple question about list of one dimensional arrays.

    Just to show you how to get one of the arrays back out of the list. Presumably once you create the list of vectors, you'll want to retrieve them. You can iterator over the list using a for loop (or foreach but I prefer the for loop):

    Qt Code:
    1. for (int i = 0; i < list.count(); i++)
    2. {
    3. QVector<int> v = list.at(i);
    4. }
    To copy to clipboard, switch view to plain text mode 

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

    robgeek (24th March 2015)

  7. #5
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Simple question about list of one dimensional arrays.

    Ok. Thanks for your help!

  8. #6
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple question about list of one dimensional arrays.

    If your arrays will always have the same length (6 in your example), then you could use std::array instead of QVector:
    Qt Code:
    1. #include <array>
    2. typedef std::array<int, 6> MyArray;
    3.  
    4. QList<MyArray> l;
    5. l.push_back(MyArray{0, 1, 2, 3, 4, 5});
    To copy to clipboard, switch view to plain text mode 
    This is C++11, but if you use an older version you can define your own MyArray by wrapping a plain C array in a class. std::array has the following advantages over QVector:
    • the fixed length (6) of all the arrays is enforced by typing;
    • std::array is more memory-efficient, because it does allocate extra storage in case the array grows, like QVector does;
    • there is no extra indirection (although QVector could implement a small array optimization).

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple question about list of one dimensional arrays.

    Isn't QVector, by definition, just a wrapper around an array of T?
    (plus a count maybe)

    Anyway, there is also QVarLengthArray

    Cheers,
    _

  10. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple question about list of one dimensional arrays.

    QVector and QVarLengthArray both manage a variable-length array, while std::array manages an array whose length is a compile-time constant. std::array has advantages at compile time (typing enforces the fixed size) and at runtime. The memory representation of std::array is just the sequence of elements, with no indirection. QVector presumably goes through an indirection, and stores sizes (current size and capacity). QVarLengthArray presumably stores the size; each access must test the current kind of representation (inlined or through an indirection).

  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple question about list of one dimensional arrays.

    I did not mean to imply that these classes were equal to std::array.

    However, the var length array will be very similar, it will also allocate the given size on the stack. An alternative if C++11 is not available.

    Not sure what you mean with indirection though.

    Cheers,
    _

  12. #10
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Simple question about list of one dimensional arrays.

    What I mean by "indirection" is an access that involves dereferencing a pointer. QVector needs at least one level of indirection since it allocates its elements on the heap, and perhaps another one due to implicit sharing. A QVarLengthArray has two modes, depending on whether it uses the storage on the stack (no indirection) or on the heap (one indirection). Even though the former mode avoids the indirection, it uselessly allocates storage for two integers and a pointer, and each access to an element must test the mode.

    QVector and QVarLengthArray are overkill for 6 integers and lack the typing guarantees of a plain array, or even
    Qt Code:
    1. struct ArrayInt6 {
    2. int elements[6];
    3. }
    To copy to clipboard, switch view to plain text mode 
    when C++11 is not an option.

Similar Threads

  1. QTestLib. A simple example. A list of the FAILS.
    By 8Observer8 in forum Newbie
    Replies: 6
    Last Post: 5th October 2013, 09:07
  2. Question About Arrays inside functions
    By rdelgado in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2011, 14:27
  3. Replies: 3
    Last Post: 11th June 2009, 04:26
  4. simple question on pointer-arrays
    By mickey in forum General Programming
    Replies: 2
    Last Post: 17th February 2007, 01:11
  5. Another simple question...
    By Dark_Tower in forum Newbie
    Replies: 5
    Last Post: 31st March 2006, 16: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
  •  
Qt is a trademark of The Qt Company.