Effective STL 阅读笔记: Item 4 ~ 5: Call empty instead of checking size() against zero.
Table of Contents
1 Item 4: Call empty instead of checking size() against zero
简而言之,用 container.size() 来检查 container 是否为空,而不要用 /container.size() == 0/。
原因: empty is a constant-time operation for all standard containers, but for some list implementations, size takes linear time.
2 Item 5: Prefer range member functions to their single-element counterparts
如果 Container 提供了区间成员函数 (Range member functions),尽量用它来替代自己写的循环和 algrithm 中的 copy 。
A range member function is a member function that, like STL algorithms, uses two iterator parameters to specify a range of elements over which something should be done.
Range member function 的好处在于:
- 代码量更少,更清晰、直观。
- 有可能会减少申请内存的次数, 减少数据的移动次数。
Range member function 适用的几种情况:
- 容器的构造: 所有标准容器都提供该方法:
container::container(InputIterator begin, // Beginning of range
InputIterator end); // End of range - 区间插入: 所有标准容器都提供该方法:
void container::insert(iterator position, // where to insert the range
InputIterator begin, // start of range to insert
InputIterator end); // end of range to insert由于关联容器 (Associative Containers) 有自己的方法来计算插入的位置,insert 方法的第一个 参数经常可以省略:
void container::insert(lnputIterator begin, InputIterator end);
- 区间删除 (Range Eraure):
所有标准容器都提供了区间删除方法,但返回值有所不同:
- Sequence Container:
iterator container::erase(iterator begin, iterator end);
- Associative Container:
void container::erase(iterator begin, iterator end);
- Sequence Container:
- 区间赋值 (Range Assignment): 所有 Container 均提供:
void container::assign(lnputIterator begin, InputIterator end);
(使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective STL 阅读笔记: Item 4 ~ 5: Call empty instead of checking size() against zero.的更多相关文章
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- 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 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Effective STL 阅读笔记: Item 3: Make copying cheap and correct for objects in containers
容器 (Containers) 用来存储对象 (Objects), 但是被存储的对象却并非原原本本是你给他的那一个, 而是你指定对象的一个拷贝.而后续对该容器内存储对象的操作,大多也是基于拷贝的. 拷 ...
- Effective STL 学习笔记 Item 17: Swap Trick
假设有若干对象存于一个 vector 中: class Widget; vector<Widget> vw; 后来由于某些原因,从该容器中删除了若干对象(参考erase-remove id ...
- Effective STL 学习笔记 Item 16:vector, string & C API
有时需要支持 C 的接口,但这并不复杂. 对于 vector 来讲, \(v[0]\) 的地址 \(\&v[0]\) 即可作为数组指针传递给 C API: 1: // Legacy C API ...
- Effective STL 学习笔记 Item 18: 慎用 vector<bool>
vector<bool> 看起来像是一个存放布尔变量的容器,但是其实本身其实并不是一个容器,它里面存放的对象也不是布尔变量,这一点在 GCC 源码中 vector<bool> ...
随机推荐
- Flash平台的分析与RIA的趋势
10月3号,Flash Player 11 和 AIR 3.0正式提供下载,一片安静.最近这两年来,关于Flash的新闻一向是以负面为主,先是 Silverlight 的挑战,然后是 iphone和i ...
- 如何调整Flash与div的相互位置
让flash置于DIV层之下的方法,让flash不挡住飘浮层或下拉菜单,让Flash不档住浮动对象或层的关键参数:wmode=opaque. 方法如下: 针对IE 在<object>< ...
- UESTC--1682
原题链接: 分析:求最小周期的应用. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- Land of Farms HDU - 5556 二分图匹配
Farmer John and his brothers have found a new land. They are so excited and decide to build new farm ...
- bzoj 2434 AC自动机+树状数组
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 3493 Solved: 1909[Submit][Sta ...
- FreeRTOSv9.0.0在STM32F103RCT6上的移植
1.去官网下载源代码(FreeRTOSv9.0.0.exe) 2.取出Source文件夹,根据单片机和编译器不同,删除不需要的文件,如下图 3.在CORTEX_STM32F103_IAR文件夹中取出P ...
- AbstractTransactionalJUnit4SpringContextTests事务回滚
在单元测试中继承AbstractTransactionalJUnit4SpringContextTests类的 时候,会默认事务回滚. 需要照常执行的话,在执行的测试方法上添加@Rollback(fa ...
- linux内核文件系统:proc、tmpfs、devfs、sysfs简要介绍
linux内核文件系统:proc.tmpfs.devfs.sysfs proc:虚拟文件系统,在linux系统中被挂载与/proc目录下.里面的文件包含了很多系统信息,比如cpu负载. 内存.网络配置 ...
- javaScript操作数组的常用方法
map(映射), reduce(规约), forEach(遍历), filter(过滤),它们都是高阶函数,都是以传入不同的函数来以不同的方式操作数组元.ie都不支持 判断是否为数组 Array.is ...
- CentOS 6.5 安装 MongoDB
1. 配置 yum 新建 /etc/yum.repos.d/mongodb-org-3.4.repo 文件,使用以下配置:(适用于 MongoDB 3.0 以后版本) [mongodb-org-3.4 ...