小编Mar*_*arc的帖子

将手写循环转换为std库调用

我最近看过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)

有没有办法重构手写循环?

c++ c++11 c++14

5
推荐指数
2
解决办法
467
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1