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

*/-->

div.org-src-container {
font-size: 85%;
font-family: monospace;
}
pre.src {
background-color:#2e3436;
color:#fefffe;
}

p {font-size: 15px}
li {font-size: 15px}

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.

Effective STL 43: Prefer algorithm calls to hand-written loops的更多相关文章

  1. C++学习书籍推荐《Effective STL(英文)》下载

    百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing train ...

  2. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

  3. [置顶] Effective STL 学习笔记

    看Effective STL 作的一些笔记,希望对各位有帮助. 以下是50条条款及相关解释. 容器 1. 慎重选择容器类型,根据需要选择高效的容器类型. 2. 不要试图编写独立于容器类型的代码. 3. ...

  4. Effective STL 中文版(大全)

    Effective STL 中文版(大全) 作者:winter 候捷说,对于STL,程序员有三个境界,开始是使用STL,然后是理解STL,最后是补充STL.Effective STL是一本非常好的书, ...

  5. C++之Effective STL

    今天看了下websocket的知识,了解到这是html5新增的特性,主要用于实时web的通信.之前客户端获取服务端的数据,是通过客户端发出请求,服务端进行响应的模式,或者通过ajax每隔一段时间从后台 ...

  6. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  7. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  8. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  9. 《Effective STL》学习笔记

    http://www.cnblogs.com/arthurliu/archive/2011/08/07/2108386.html 作者:咆哮的马甲 出处:http://www.cnblogs.com/ ...

随机推荐

  1. 哲学家问题(java)的三个解法

    //加synchronize进行同步 //释放资源又很快获得自身的资源,这样不妥,吃完的话休息100ms //每个人先申请编号小的筷子 public class Philosopher impleme ...

  2. c++ 容器弊端

    1.stack 不能直接清空,要 while (!s.empty()) s.pop(); 2.vector 增添.删除数据,也许vector首尾的地址会发生改变 如: ( watch f.begin( ...

  3. PHP_EOL 写入字符串换行 , php获取毫秒 microtime

    private function miclog($t1,$t2,$name){ $lasttime = ($t2 - $t1).'ms'; $content = date('Y-m-d H:i:s', ...

  4. Java5的新特性

    原文出处:xixicat 序 这是Java语言特性系列的第一篇,从java5的新特性开始讲起.初衷就是可以方便的查看语言的演进历史. 特性列表 泛型 枚举 装箱拆箱 变长参数 注解 foreach循环 ...

  5. Java基础-面向对象第一特性之封装(Encapsulation)

    Java基础-面向对象第一特性之封装(Encapsulation) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.理解什么是面向过程和面向对象 面向过程与面向对象都是我们编程中 ...

  6. Vue 嵌套数组 数组更新视图不更新

    关于Vue的响应式原理,可以看官方文档或其他资料, https://www.jianshu.com/p/34de360d6035 data里定义了一个数组arr,数组的元素可以是同样格式的数组arrC ...

  7. [HNOI 2013]切糕

    COGS 2398. [HNOI 2013]切糕 http://www.cogs.pro/cogs/problem/problem.php?pid=2398 ★★★☆   输入文件:nutcake.i ...

  8. TOMCAT添加管理用户认证

    添加配置文件 --原配置文件: # tail -5 /usr/local/tomcat/conf/tomcat-users.xml <user username="tomcat&quo ...

  9. Java并发编程原理与实战五:创建线程的多种方式

    一.继承Thread类 public class Demo1 extends Thread { public Demo1(String name) { super(name); } @Override ...

  10. CSS3实战-文字篇

    text-shadow的大作用 多颜色阴影效果,用逗号分隔text-shaodow即可. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...