我在这里有一个递归的函数,但我宁愿让它非递归.我只是不确定如何.
void AguiWidgetManager::recursiveRender(const AguiWidget *root)
{
//recursively calls itself to render widgets from back to front
AguiWidget* nonConstRoot = (AguiWidget*)root;
if(!nonConstRoot->isVisable())
{
return;
}
clip(nonConstRoot);
nonConstRoot->paint(AguiPaintEventArgs(true,graphicsContext));
for(std::vector<AguiWidget*>::const_iterator it =
root->getPrivateChildBeginIterator();
it != root->getPrivateChildEndIterator(); ++it)
{
recursiveRender(*it);
}
for(std::vector<AguiWidget*>::const_iterator it =
root->getChildBeginIterator();
it != root->getChildEndIterator(); ++it)
{
recursiveRender(*it);
}
}
Run Code Online (Sandbox Code Playgroud)
如果解决方案不适用于迭代器,那也没关系.
谢谢
简单的方法就是维护自己的堆栈.Psuedoish示例代码:
stack s;
while(!s.empty())
{
root = s.pop();
//your code here
//replace each recursive call with s.push(it)
}
Run Code Online (Sandbox Code Playgroud)
另外,抛弃常量是一个糟糕的坏主意.如果你想修改它,它首先不应该是一个const参数.