void Mainframe::OnClearNotebook( wxCommandEvent& e )
{
int end = m_notebook->GetPageCount();
for ( int i = 0; i < end; i++ )
{
if ( m_notebook->GetPageText( i ) != "Server Log" )
{
m_notebook->DeletePage(i);
}
}
}
此代码在此表单中崩溃并显示消息“m_notebook->GetPageText() 索引超出范围”;
如何正确地遍历所有页面?
编辑,解决方案:
void Mainframe::OnClearNotebook( wxCommandEvent& e )
{
while ( m_notebook->GetPageCount() > 1 )
{
int end = m_notebook->GetPageCount() - 1;
if ( m_notebook->GetPageText( end ) != "Server Log" )
{
m_notebook->DeletePage(end);
}
else
{
break;
}
}
}
最佳答案
如果您实际删除,GetPageCount() 将被更改,因此 end 将保留早期版本的值
例如。
你在 m_notebook->GetPageCount() 中有 100
所以结束 = 100
在迭代 10 个项目后,您将删除 5 个项目,所以现在列表将有 95 个项目,但您迭代到 100 个 - 这可能是问题
关于c++ - 如何遍历 wxNotebook 的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151343/