Results 1 to 2 of 2

Thread: C++ - recursion for loop - how to get access to various iterators

  1. #1
    Join Date
    May 2017
    Posts
    13
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default C++ - recursion for loop - how to get access to various iterators

    I would like to make recursion in for loop with access to following iterators independently.
    I tried to do that, but it looks like nested loop doesn't update its parent iterator. I am not sure what's wrong here:

    Qt Code:
    1. std::vector<int> it;
    2. // some code that fill "it" vector
    3.  
    4. std::vector<int *> iter;
    5. for(int i=0; i<it.size(); i++)
    6. {
    7. iter.push_back(&it(i));
    8. }
    9.  
    10. int sweek = 0;
    11. int dup=0;
    12.  
    13. // And now call the function which definition is:
    14. void matrixRecursion()
    15. {
    16. int start = sweek;
    17. int end = matrix[sweek];
    18.  
    19. for(it(start); it(start)<end; it(start)++)
    20. {
    21. if(sweek < it.size()-1)
    22. {
    23. sweek++;
    24. matrixRecursion();
    25. }
    26. else
    27. {
    28. for(int t=0; t<it.size(); t++)
    29. {
    30. dup += nIndex[*iter[t]];
    31. }
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    For example lets say `it.size()` is 3. Then if everything would work as I expect the result should be like that:

    Qt Code:
    1. for(int i=0; i<matrix[0]; i++)
    2. {
    3. for(int j=0; j<matrix[1]; j++)
    4. {
    5. for(int k=0; k<matrix[2]; k++)
    6. {
    7. dup += nIndex[i] + nIndex[j] + nIndex[k];
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: C++ - recursion for loop - how to get access to various iterators

    for(it(start); it(start)<end; it(start)++)
    Maybe you should explain what you think this line of code is supposed to do. std::vector doesn't have an operator()( int ). Even if it did, what do you expect this code to do? "it" is a vector, and even if "it( start )" returned some member of that vector, your for() loop doesn't save it anywhere so it can use it later so the statement is essentially a no-op.

    I think you are basically confused about what iterators are and how you use them. In any case, this post is not about Qt, it is basic C++ knowledge and is off-topic for this forum.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to access main loop
    By roseicollis in forum Newbie
    Replies: 6
    Last Post: 22nd December 2014, 11:22
  2. Filtering iterators
    By akiross in forum Qt Programming
    Replies: 18
    Last Post: 2nd September 2011, 01:08
  3. Recursion Problem
    By kingfinn in forum Qt Programming
    Replies: 0
    Last Post: 23rd January 2010, 22:15
  4. access the members of the Form from a loop.
    By cbarmpar in forum Qt Programming
    Replies: 2
    Last Post: 25th September 2008, 02:35
  5. QLinkedList and iterators
    By eu.x in forum Newbie
    Replies: 1
    Last Post: 19th April 2007, 20:38

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.