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> ...
随机推荐
- [pool www] user has not been defined
[02-Dec-2014 00:28:58] ALERT: [pool www] user has not been defined [02-Dec-2014 00:28:58] ERROR: fai ...
- 【loj2033】生成魔咒
Portal --> loj2033 Solution 这题..虽然说好像也是sam的裸题不过既然在智力康复那就强制后缀数组吧qwq (晚点再用sam写一次qwq) 首先如果是要求本质不同的 ...
- _MSC_VER
https://msdn.microsoft.com/en-us/library/vstudio/b0084kay.aspx Evaluates to an integer literal that ...
- 树莓派更改vnc分辨率
参考链接:https://raspberrypi.stackexchange.com/questions/56421/how-to-increase-resolution-on-latest-rasp ...
- Linux环境编译动态库和静态库总结
对Linux环境动态库和静态库的一些基础知识做一些总结, 首先总结静态库的编译步骤. 1 先基于.cpp或者.c文件生成对应的.o文件 2将几个.o文件 使用ar -cr命令 生成libname.a文 ...
- Java设计模式の责任链模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...
- LeetCode-Reverse Words in a String[AC源码]
package com.lw.leet1; import java.util.Stack; /** * @ClassName:Solution * @Description: * Reverse Wo ...
- cmd下常用命令汇总
1.获得文件夹内所有文件的文件名列表 dir *.png /b>list.txt 其中: (1)*.png表示筛选后缀为.png的文件 (2)/b为输出的模式.如下: 引用 /b 只有文件名 ...
- Python输出字符串或文件颜色显示
书写格式,和相关说明如下: 格式:\033[显示方式;前景色;背景色m 说明: 前景色 背景色 颜色 --------------------------------------- 30 40 黑色 ...
- ASP.NET网站伪静态下使用中文URL
首先解释一下,什么是中文URL呢?它并不是我们常见的把汉字编码为 %CF%EC 这种形式,而是在URL中直接使用汉字 这种形式目前还不是很多见.因为不同的浏览器处理起来可能会有所不同,不过据我测试,I ...