PDA

View Full Version : Multi Dimensional QList



aamer4yu
30th March 2007, 12:18
Hi, This must be an easy prob,,,, but my head's not working much today, so posting the query :D

I want to have a 2D array of integers using QList,
something like QList< QList<int> > > twoDArray; The main purpose of creating the array thru QList is that i dont have the dimensions at the time of declaration. They have to be populated later.

Do I have to iterate each and every element of twoDArray and initialize it witht he correct value ?? Will QVector be better ??

Can someone show a example for the same ?

patrik08
30th March 2007, 12:48
Hi, This must be an easy prob,,,, but my head's not working much today, so posting the query :D

I want to have a 2D array of integers using QList,
something like QList< QList<int> > > twoDArray; The main purpose of creating the array thru QList is that i dont have the dimensions at the time of declaration. They have to be populated later.

Do I have to iterate each and every element of twoDArray and initialize it witht he correct value ?? Will QVector be better ??

Can someone show a example for the same ?

I love QDomDocument .... populated new level any time && any place and grab fast result... binary or text ....

Eldritch
30th March 2007, 17:46
By definition, accesses to QList are O(n), and yes, initializing elements will mean visiting every one indivually -- if you need to change them after inserting them to the list.

Vectors are resizeable, but the cost of resizing can be higher than with a list. That's the root of the problem -- which dimension of performance is most important? Access speed or resizing speed? The constant time access of a vector is invaluable for large data sets.

Also, remember that a QVector can be initially sized to zero, and you can push elements at the the front or back very much like a list. Presuming even some simplistic optimizations (and I'm betting it's better), adding / removing elements on a QVector won't have the performance penalties that a brain-dead malloc / memcpy / free approach would.

jacek
30th March 2007, 17:55
By definition, accesses to QList are O(n),
Not exactly, as QList is not a typical list, but rather a kind of general purpose container.

See:
http://doc.trolltech.com/4.2/containers.html#algorithmic-complexity

aamer4yu
2nd April 2007, 05:35
but how do I initialise the list to some size....
let me repeat my question more specifically...

Say I have the following class -

class MyClass
{
QList<QList<int> > 2dArray;
public :
MyClass()
{
// read row col from file....
// set size of Qlist to row and col
}
friend somefunction();
};

somefunction()
{
MyClass objMyClass;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
objMyClass.2darray[i][j] = ;// assign some value
}



here in the myclass i need to know the size of the array in the ctor and allocate the size to the list. so that later when i call any elemnt of the list, i can use it properly... QList gives error if i try to access index out of bounds...

i know i can use pointers for the problem... but just trying with QList or QVector...

aamer4yu
2nd April 2007, 06:46
done it atlast... :)

in
MyClass()
{
// read row col from file / other class etc
2dArray = QVector<QList<int> >(rows).toList();
for(i=0;i<row;i++)
2dArray[i] = QVector<int>(cols).toList();
}

this helps to allocate the space and its working.... not sure if there are any pitfalls...there might be

any comments ??