Effective STL 43: Prefer algorithm calls to hand-written loops
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.
(转载请注明出处,使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective STL 43: Prefer algorithm calls to hand-written loops的更多相关文章
- C++学习书籍推荐《Effective STL(英文)》下载
百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing train ...
- 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 ...
- [置顶] Effective STL 学习笔记
看Effective STL 作的一些笔记,希望对各位有帮助. 以下是50条条款及相关解释. 容器 1. 慎重选择容器类型,根据需要选择高效的容器类型. 2. 不要试图编写独立于容器类型的代码. 3. ...
- Effective STL 中文版(大全)
Effective STL 中文版(大全) 作者:winter 候捷说,对于STL,程序员有三个境界,开始是使用STL,然后是理解STL,最后是补充STL.Effective STL是一本非常好的书, ...
- C++之Effective STL
今天看了下websocket的知识,了解到这是html5新增的特性,主要用于实时web的通信.之前客户端获取服务端的数据,是通过客户端发出请求,服务端进行响应的模式,或者通过ajax每隔一段时间从后台 ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 《Effective STL》学习笔记
http://www.cnblogs.com/arthurliu/archive/2011/08/07/2108386.html 作者:咆哮的马甲 出处:http://www.cnblogs.com/ ...
随机推荐
- 关于nodejs的几个干货(读中文文件编码问题/发送邮件/定时任务)
关于nodejs读取中文文件真是折腾了不少时间,网上各种方案,最后没有一个适用我,好在解决了. 下面的三个知识点都是从项目中抽出的,要单独运行脚本的话需要用全局模式来安装模块,比如安装中文转换模块(后 ...
- supervisor "INFO spawnerr: unknown error making dispatchers for xxx" 错误
查看 supervisor 的配置中,是否有受限的文件路径,如果有,则需要修改对应文件的权限
- Hystrix之Dashboard的常见问题
Hystrix Dashboard (断路器:Hystrix 仪表盘)只监控一个实例,而Turbine监控多个实例,要使用Turbine必须使用Hystrix,因为Turbine是为了监控断路器的状态 ...
- Chapter 5(串)
1.kmp #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <str ...
- ssh 执行命令并实时显示结果
ssh 执行命令并实时显示结果 import paramiko def main(): sshClient = paramiko.SSHClient() sshClient.set_missing_h ...
- vue全局组件和局部组件
1.全局注册组件 Vue.componet('name',{ template:'<div></div>', data(){ retrun {} } }) 使用了以上这种方式注 ...
- tensorflow变量作用域(variable scope)
举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时候共享变量会无比复杂. 官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望 ...
- vs配置SP++3.0
最近在研究信号处理的C语言算法,突然发现一个西安交大的师兄之前已经写出来了一个完整的库,同样是研究生三年,差别怎么这样大呢. 先从用轮子开始吧. 一.SP++3.0安装及测试 官网下载地址: http ...
- 深入浅出CSS(三):隐藏BOSS大盘点之默认属性小总结
写在前面 严重警告,本文包含大量文字,且无配图,请做好充分心理准备后,再进行阅读! 严重警告,本文包含大量文字,且无配图,请做好充分心理准备后,再进行阅读! 严重警告,本文包含大量文字,且无配图,请做 ...
- 我购买byd的几点逻辑
1.伯克希尔哈撒韦长期看好byd不是无道理的,每次转型都是那么的成功,说明管理层很好. 2.2015年6月员工持股计划均价55元,目前48元. 3.新能源汽车龙头. 4.云轨解决了小城市建设地铁过于浪 ...