[SOLVED]Use of Iterator to access a QList
Hi,
Here is the code:
Code:
#include <QDebug>
#include <QList>
class Node
{
public:
int a;
int b;
};
int main(int argc, char *argv[])
{
//case1
QList<int> list1;
list1.append(10);
list1.append(3);
QList<int>::iterator i;
for (i = list1.begin(); i != list1.end(); ++i)
qDebug() << *i;
//case2
QList<Node> list2;
Node no = {10, 3};
list2.append(no);
QList<Node>::iterator j;
for (j = list2.begin(); j != list2.end(); ++j)
//here ???
;
}
In case 2, how can i access the a and b from each node?
Thanks
Re: Use of Iterator to access a QList
But I would preferre the index based approach for lists...
Re: Use of Iterator to access a QList
Good work, asking with a minimal compilable program!
Code:
//case2
QList<Node> list2;
Node no = {11, 12};
list2.append(no);
QList<Node>::iterator j;
for (j = list2.begin(); j != list2.end(); ++j)
{
qDebug() << (*j).a << (*j).b;
}
HIH
Johannes
Re: Use of Iterator to access a QList
humm ... are you certain of this?
Re: Use of Iterator to access a QList
ahhh ... ok ...
Code:
qDebug() << (*j).a << (*j).b;
TANKS
Re: Use of Iterator to access a QList
Damn, of course the brackets...:p
Re: Use of Iterator to access a QList
Just wanted to apologize for the cross/double post.. but, yes, that's right. You need the brackets.
You should consider, using a constructor for your class, which initializes those properties. Or use a struct and plain old c. :->
Code:
#include <QApplication>
#include <QDebug>
#include <QList>
class Node
{
public:
int a;
int b;
};
int main(int argc, char *argv[])
{
//case1
QList<int> list1;
list1.append(10);
list1.append(3);
QList<int>::iterator i;
for (i = list1.begin(); i != list1.end(); ++i)
qDebug() << *i;
//case2
QList<Node> list2;
Node no = {11, 12};
list2.append(no);
QList<Node>::iterator j;
for (j = list2.begin(); j != list2.end(); ++j)
{
qDebug() << (*j).a << (*j).b;
}
return 0;
}
Johannes
Re: Use of Iterator to access a QList
Again :-> I should type faster!
Re: Use of Iterator to access a QList
How about
Code:
qDebug() << j->a << j->b;
Re: Use of Iterator to access a QList
Quote:
Originally Posted by
JohannesMunk
Again :-> I should type faster!
I'm a little slow today also!
Re: Use of Iterator to access a QList
Slow, but you contributed something new, anyhow :->
But as the iterator itself, is not a pointer type, that might seem strange..
It has its *-operator overloaded to return the item, and (*a). is equivalent to a-> .. compiler magic.
Well its a matter of taste, I guess.
Joh