Results 1 to 11 of 11

Thread: [SOLVED]Use of Iterator to access a QList

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [SOLVED]Use of Iterator to access a QList

    Hi,
    Here is the code:
    Qt Code:
    1. #include <QDebug>
    2. #include <QList>
    3.  
    4. class Node
    5. {
    6. public:
    7. int a;
    8. int b;
    9. };
    10. int main(int argc, char *argv[])
    11. {
    12. //case1
    13. QList<int> list1;
    14. list1.append(10);
    15. list1.append(3);
    16. QList<int>::iterator i;
    17. for (i = list1.begin(); i != list1.end(); ++i)
    18. qDebug() << *i;
    19.  
    20. //case2
    21. QList<Node> list2;
    22. Node no = {10, 3};
    23. list2.append(no);
    24. QList<Node>::iterator j;
    25. for (j = list2.begin(); j != list2.end(); ++j)
    26. //here ???
    27. ;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    In case 2, how can i access the a and b from each node?
    Thanks
    Last edited by graciano; 22nd March 2010 at 16:06.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Use of Iterator to access a QList

    Qt Code:
    1. *i.a;
    To copy to clipboard, switch view to plain text mode 
    But I would preferre the index based approach for lists...

  3. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use of Iterator to access a QList

    humm ... are you certain of this?

  4. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Use of Iterator to access a QList

    ahhh ... ok ...
    Qt Code:
    1. qDebug() << (*j).a << (*j).b;
    To copy to clipboard, switch view to plain text mode 

    TANKS

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Use of Iterator to access a QList

    Damn, of course the brackets...

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Use of Iterator to access a QList

    How about
    Qt Code:
    1. qDebug() << j->a << j->b;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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. :->

    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QList>
    4.  
    5. class Node
    6. {
    7. public:
    8. int a;
    9. int b;
    10. };
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QApplication app(argc, argv);
    15. //case1
    16. QList<int> list1;
    17. list1.append(10);
    18. list1.append(3);
    19. QList<int>::iterator i;
    20. for (i = list1.begin(); i != list1.end(); ++i)
    21. qDebug() << *i;
    22.  
    23. //case2
    24. QList<Node> list2;
    25. Node no = {11, 12};
    26. list2.append(no);
    27. QList<Node>::iterator j;
    28. for (j = list2.begin(); j != list2.end(); ++j)
    29. {
    30. qDebug() << (*j).a << (*j).b;
    31. }
    32. return 0;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Johannes

  9. #9
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of Iterator to access a QList

    Again :-> I should type faster!

  10. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Use of Iterator to access a QList

    Quote Originally Posted by JohannesMunk View Post
    Again :-> I should type faster!
    I'm a little slow today also!

  11. #11
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of Iterator to access a QList

    Good work, asking with a minimal compilable program!

    Qt Code:
    1. //case2
    2. QList<Node> list2;
    3. Node no = {11, 12};
    4. list2.append(no);
    5. QList<Node>::iterator j;
    6. for (j = list2.begin(); j != list2.end(); ++j)
    7. {
    8. qDebug() << (*j).a << (*j).b;
    9. }
    To copy to clipboard, switch view to plain text mode 

    HIH

    Johannes

  12. The following user says thank you to JohannesMunk for this useful post:

    graciano (22nd March 2010)

Similar Threads

  1. segfault while trying to access QList
    By Raven24 in forum General Programming
    Replies: 1
    Last Post: 3rd March 2010, 06:53
  2. pointer and iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 3rd February 2008, 22:24
  3. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 16:15
  4. iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 23rd May 2006, 10:28
  5. ::iterator
    By mickey in forum General Programming
    Replies: 2
    Last Post: 20th March 2006, 22:05

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.