我最近看过Sean Parent关于2013年C++调味的讲话.如果我理解他,他说你可以从代码中消除几乎所有(全部?)手写循环.我的问题是如何实现这一目标?我们考虑以下代码:
class ProgressDialog
{
//interesting part of that class
void SetPosition(int position);
bool IsCancelRequested();
void SetHeader(const std::string& status);
}
void foo( const std::vector<std::string>& v)
{
ProgressDialog dlg;
long position = 0;
for( const auto& s : v)
{
++position;
dlg.SetPosition(position);
dlg.SetHeader("Processing"+ s);
DoSomethingThatTakesSomeTime(s);
if(dlg.IsCancelRequested()) break;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法重构手写循环?