Iteration to recursive function
You can modify the recursive code to use an explicit stack:

#include &lt;stack&gt;

void display_widgets(control&amp; controls)
{
    std::stack&lt;widget*&gt; widgets;
    for(auto &amp;widget : controls.getWidgets()) {
        widgets.push(widget);
    }

    while(!widgets.empty()) {
        widget* w = widgets.top();
        widgets.pop();
        if(!widget-&gt;visible) {
            continue;
        }
        widget-&gt;draw();
        for(auto&amp; component : widget-&gt;components) {
            widgets.push(component);
        }
    }
}



  I know iteration is better.


This is highly dependent on language and usage. It is "better" in that recursion can use O(n) stack space (although not always - tail call optimization gets rid of this limitation), and won't blow your stack. However, in this case, assuming the recursive algorithm actually did what you wanted, it would be better and easier. 

You can always turn a recursive solution into an iterative one by replacing the call stack with an actual stack of your own, as above. For very deeply nested recursive calls, this may save you from stack overflows, however, it is more fiddly to code, and in this case, doesn't gain you anything. 

Having written the code above, my advice would be to stick with the recursive solution.