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. Linux下的wine生活(QQ/微信/Office)

    My wine life like windows 本篇内容涉及QQ.微信.Office在wine中的使用配置. QQ 到deepin下载轻聊版. 如果安装了crossover,那么将其中opt/cx ...

  2. pg数据库杀掉连接

    遇到异常连接时,需要将对应连接杀掉,可能是连接占用了过多CPU或是IO,影响了业务,或是时间过长的空闲事务. pg对于杀掉连接提供了专门的命令,一般情况下使用pg_cancel_backend就可以, ...

  3. docker:轻量级图形页面管理之Portainer

    docker:轻量级图形页面管理之Portainer 原创甘兵2018-03-05 14:26:56评论(8)2586人阅读   1.介绍 docker 图形化管理提供了很多工具,有Portainer ...

  4. 加ico

    <link rel="icon" type="text/css" href="/favicon.png" />

  5. Hadoop基础-Apache Avro串行化的与反串行化

    Hadoop基础-Apache Avro串行化的与反串行化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Apache Avro简介 1>.Apache Avro的来源 ...

  6. Mongodb 笔记01 MongoDB 简介、MongoDB基础知识、启动和停止MongoDB

    MongoDB 简介 1. 易于使用:没有固定的模式,根据需要添加和删除字段更加容易 2. 易于扩展:MongoDB的设计采用横向扩展.面向文档的数据模型使它能很容易的再多台服务器之间进行分割.自动处 ...

  7. SQL语句(十七)综合练习_分组查询_内嵌查询_视图使用

    Select * from Student Select * From Course Select * from SC --子查询 低于总平均成绩的女同学成绩 Select Grade from St ...

  8. Git之简介及安装

    简介 Git是一个分布式版本控制系统,GitHub相当于一个远程仓库,注册账号可免费获得Git远程仓库. GitHub使用参考:https://guides.github.com/activities ...

  9. GitHub更新已经fork的项目

    clone 自己的 fork 分支到本地 可以直接使用 GitHub 客户端,clone 到本地,如果使用命令行,命令为: $ git clone git@github.com:morethink/g ...

  10. 【Foreign】Rectangle [KD-tree]

    Rectangle Time Limit: 50 Sec  Memory Limit: 512 MB Description Input Output Sample Input 0 4 2 0 2 1 ...