Results 1 to 4 of 4

Thread: Loop or Nested Iterator

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Loop or Nested Iterator

    I would like to use QHashIterator (or any of the iterators) in a loop. how to I re-assign the object the iterator is iterating over? I would need to do this for a nested iterator too.

    Example:
    Qt Code:
    1. QHash<QString, QHash<int,float> > testhash;
    2.  
    3. // code to populate hashes
    4.  
    5. QHashIterator<QString, QHash<int,float> > i(testhash);
    6. while (i.hasNext())
    7. {
    8. i.next();
    9. QHashIterator<int,float> j(i.value());
    10. while (j.hasNext())
    11. {
    12. //get data
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    How do I reinitialize j in every loop? If I leave the code as is, j will have extra values.
    Last edited by enricong; 27th September 2011 at 23:13.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Loop or Nested Iterator

    "j" is created every time through the outer while loop and ceases to exist before the next loop starts. There is no "reinitialize" they are separate instances in each outer loop. I have no idea what you mean by "j will have extra values". This:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. QHash<QString, QHash<int,float> > testhash;
    5.  
    6. int main(int argc, char **argv) {
    7. QCoreApplication app(argc, argv);
    8.  
    9. // code to populate hashes
    10. for (int i = 0; i < 4; ++i ) {
    11. QString key = QString("Key %1").arg(i);
    12. testhash[key] = QHash<int,float>();
    13. for (int j = 0; j < 3; ++j) {
    14. testhash[key][j] = j * 3.0 ;
    15. }
    16. }
    17.  
    18. QHashIterator<QString, QHash<int,float> > i(testhash);
    19. while (i.hasNext())
    20. {
    21. i.next();
    22. qDebug() << "Outer key" << i.key();
    23. QHashIterator<int,float> j(i.value());
    24. while (j.hasNext())
    25. {
    26. j.next();
    27. //get data
    28. qDebug() << "Inner" << j.key() << j.value();
    29. }
    30. }
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 
    outputs nothing unexpected.
    Qt Code:
    1. Outer key "Key 0"
    2. Inner 0 0
    3. Inner 1 3
    4. Inner 2 6
    5. Outer key "Key 1"
    6. Inner 0 0
    7. Inner 1 3
    8. Inner 2 6
    9. Outer key "Key 2"
    10. Inner 0 0
    11. Inner 1 3
    12. Inner 2 6
    13. Outer key "Key 3"
    14. Inner 0 0
    15. Inner 1 3
    16. Inner 2 6
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Loop or Nested Iterator

    That is what I thought (that there is no need to reinitialize).

    However, using the debugger, if I set a breakpoint inside the inner loop and look at "j" in each look and compare it to testhash, j will sometimes contain more elements.

    for example, if I expect the following:
    Qt Code:
    1. outer key 1:
    2. 0 10
    3. 1 11
    4. 2 12
    5. 3 13
    6. 4 14
    7. outer key 2
    8. 0 100
    9. 1 111
    To copy to clipboard, switch view to plain text mode 


    I get something like
    Qt Code:
    1. outer key 1:
    2. 0 10
    3. 1 11
    4. 2 12
    5. 3 13
    6. 4 14
    7. outer key 2
    8. 0 100
    9. 1 111
    10. 2 12
    11. 3 13
    12. 4 14
    To copy to clipboard, switch view to plain text mode 


    It seems like I am getting extra keys and it seems to match with other elements in outer keys.
    I'll have to try a simple example and see if the problem still exists and see where I'm screwing up.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Loop or Nested Iterator

    You will also get extra entries if you use hash[key] syntax for a key that does not exist, but those entries would be zero (default constructed) unless you set them.

    The problem may lie in building the hash of hashes in the first place... perhaps you are not creating a new internal hash but using one that persists.

Similar Threads

  1. Replies: 4
    Last Post: 6th August 2011, 01:40
  2. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  3. iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 23rd May 2006, 10:28
  4. ::iterator
    By mickey in forum General Programming
    Replies: 2
    Last Post: 20th March 2006, 22:05
  5. convert iterator
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th March 2006, 21:59

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.