PDA

View Full Version : Simple question about list of one dimensional arrays.



robgeek
24th March 2015, 01:48
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.

QList<int> matrix; //Did not work like what i want.
<QList matrix[6]; //Same thing as above;

jefftee
24th March 2015, 02:29
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:



int array[6] = {0,1,2,3,4,5};

QVector<int> vec;
vec.push_back(array[0]);
vec.push_back(array[1]);
vec.push_back(array[2]);
vec.push_back(array[3]);
vec.push_back(array[4]);
vec.push_back(array[5]);

QList<QVector<int> > list;

list.push_back(vec);

QVector<int> v = list.at(0);

robgeek
24th March 2015, 02:37
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.

jefftee
24th March 2015, 02:41
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):



for (int i = 0; i < list.count(); i++)
{
QVector<int> v = list.at(i);
}

robgeek
24th March 2015, 02:55
Ok. Thanks for your help!

yeye_olive
24th March 2015, 09:12
If your arrays will always have the same length (6 in your example), then you could use std::array instead of QVector:


#include <array>
typedef std::array<int, 6> MyArray;

QList<MyArray> l;
l.push_back(MyArray{0, 1, 2, 3, 4, 5});

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

anda_skoa
24th March 2015, 11:22
Isn't QVector, by definition, just a wrapper around an array of T?
(plus a count maybe)

Anyway, there is also QVarLengthArray

Cheers,
_

yeye_olive
24th March 2015, 13:58
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).

anda_skoa
24th March 2015, 15:06
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,
_

yeye_olive
24th March 2015, 15:45
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


struct ArrayInt6 {
int elements[6];
}

when C++11 is not an option.