zoukankan      html  css  js  c++  java
  • Effective STL 43: Prefer algorithm calls to hand-written loops

    Effective STL 43: Prefer algorithm calls to hand-written loops

    Suppose you have a Widget class that supports redrawing:

    class Widget
    {
    public:
        Widget();
        virtual ~Widget();
        void redraw() const;
    };
    

    and you'd like to redraw all the Widgets in a list, you could do it within a loop:

    list<Widget> lw;
    // ...
    for (list<Widget>::iterator i = lw.begin(); i != lw.end(); ++i)
    {
        i->redraw();
    }
    

    But you could also do it with the for_each algorithm:

    for_each(lw.begin(), lw.end(), mem_fun_ref(&Widget::redraw));
    

    Why should we prefer algorithm to writing our own loop? Here are the reasons:

    • Efficiency:
      Algorithms are offten more efficient than the loops programmers produce.
    • Correctness:
      writing loops is more suject to errors than is calling algorithms.
    • maintainability:
      Algorithm calls often yield code that is clear and more straightforward than the corresponding explicit loops.
  • 相关阅读:
    junit4+spring3.0.4.RELEASE测试单元基本实现
    PL/SQL 报错Dynamic Performance Tables not accessible XXX
    Oracle简单建立表空间
    R语言实现统计 plink格式数据位点缺失率
    linux shell实现统计 位点缺失率
    linux shell 统计plink格式样本缺失率
    Rstudio如何设置默认的工作路径
    如何在dos窗口中执行R脚本
    syntax error: unexpected end of file
    R语言如何删除目录下同一类型的文件、或者所有文件
  • 原文地址:https://www.cnblogs.com/yangyingchao/p/3442213.html
Copyright © 2011-2022 走看看