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. Chapter 4(栈与队列)

    1.栈的顺序存储结构 //*********************************stack_array.h************************************ #ifn ...

  2. ElasticStack系列之十一 & 同步 mysql 数据的实践与思考

    问题 1. jdbc-input-plugin 只能实现数据库的追加,对于 elasticsearch 增量写入,但经常 jdbc 源一端的数据库可能会做数据库删除或者更新操作.这样一来数据库与搜索引 ...

  3. 有关并查集的emmmm

    并查集 顾名思义,并查集有三个用处 并,即合并两个集合 查,查询该元素所在的集合 集,就指集合 现在来说一说并查集的基本操作: - 初始化 首先,最开始的时候,我们假设所有的集合都只有一个元素,即只有 ...

  4. bzoj千题计划172:bzoj1192: [HNOI2006]鬼谷子的钱袋

    http://www.lydsy.com/JudgeOnline/problem.php?id=1192 1,2,4,8,…… n-2^k 可以表示n以内的任意数 若n-2^k 和 之前的数相等,一个 ...

  5. NOIP2016 组合数问题

    https://www.luogu.org/problem/show?pid=2822 题目描述 组合数表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以 ...

  6. 贪心问题:区间覆盖 NYOJ 喷水装置(二)

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水 ...

  7. 有用的Javascript,长期更新...

    1,点击目标区域以外隐藏,运用场景:点击遮罩层,弹层关闭. // 点击目标区域以外隐藏 $(document).on("click", function (event) { var ...

  8. Web应用开发中的几个问题

    Introduction 由于Ajax技术在Gmail中的成功应用和高性能的V8引擎的推出使得编写Web应用变得流行 起来,使用前端技术也可以编写具有复杂交互的应用.相对于native应用,Web应用 ...

  9. 2016-2017-2 《Java程序设计》第五周学习总结

    20155223 2006-2007-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 try.catch语法:若程序发生错误,执行流程就调离错误点,然后比较catch括号 ...

  10. URAL 1416 Confidential (最小生成树+次小生成树)

    Description Zaphod Beeblebrox - President of the Imperial Galactic Government. And by chance he is a ...