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. jquery如何实现点击LI标签和下面的LI互换顺序? 超简单代码

    转: jquery如何实现点击LI标签和下面的LI互换顺序? 上面的效果涉及jquery的两个方法: next()  :  获得匹配元素集合中每个元素紧邻的下一个同胞元素. after() :在被选元 ...

  2. BRIEF特征点描述子

    简介 BRIEF是2010年的一篇名为<BRIEF:Binary Robust Independent Elementary Features>的文章中提出,BRIEF是对已检测到的特征点 ...

  3. Java入门系列:处理Json格式数据

    本节主要讲解: 1)json格式数据处理方法 2)第三方工具包的使用方法 3)java集合数据类型 [项目任务] 编写一个程序,显示未来的天气信息. [知识点解析] 为了方便后面代码的分析,先需要掌握 ...

  4. 我们使用git checkout 将B分支上的部分页面代码 添加或覆盖到A分支上

    $ git branch * A B $ git checkout B message.html message.css message.js other.js $ git status # On b ...

  5. .gitignore 无效问题

    利用.gitignore过滤文件,如编译过程中的中间文件,等等,这些文件不需要被追踪管理. 现象: 在.gitignore添加file1文件,以过滤该文件,但是通过Git status查看仍显示fil ...

  6. Docker入门与应用系列(四)网络管理

    一.Docker的五种网络模式 在使用docker run创建docker容器时,可以用--net选项指定容器的网络模式,Docker有以下5种网络模式: 1. bridge模式 使用docker r ...

  7. MAC下 Apache服务器配置

    今天做了一个注册登录提交的页面,后续操作需要用到后端的知识 php+Mysql,之前只是有些了解,现在开始具体操作了,首先从配置环境开始.查了好几篇文档与博客,了解了挺多知识. Mac下Apache服 ...

  8. JavaScript setInterval 与 setTimeout 区别

    setInterval:一直循环调用函数,不会停止:需要用 clearInterval 去停止 setTimeout:只调用一次

  9. Python之路,Day2 - Python基础,列表,循环

    1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...

  10. mysql 复制表数据,表结构的3种方法

    什么时候我们会用到复制表?例如:我现在对一张表进行操作,但是怕误删数据,所以在同一个数据库中建一个表结构一样,表数据也一样的表,以作备份.如果用mysqldump比较麻烦,备份.MYD,.MYI这样的 ...