使用reserve来避免不必要的重新分配!

The reserve member function allows you to minimize the number ofreallocations that must be performed, thus avoiding the costs of reallocationand iterator/pointer/reference invalidation.

使用size函数获取容器中的元素个数,使用capacity函数获取容器已分配的内容可以存储元素的个数。

对应上述两个函数:resize和reserve分别用来改变上述两个值。

Create a vector<int> holding thevalues 1–1000.

Without using reserve, you might do it like this:

 vector<int> v;
for (int i = ; i <= ; ++i) v.push_back(i);

This code will typically result in between two and 18 reallocations during the course of the loop.

 vector<int> v;
int changeNum = ;
int cap = v.capacity();
for (int i = ; i <= ; ++i)
{
v.push_back(i);
if (cap != v.capacity())
{
cap = v.capacity();
changeNum++;
}
} //Yes, the changeNum is 18!

Modify the code to use reserve gives us this:

 vector<int> v;
v.reserve();
for (int i = ; i <= ; ++i) v.push_back(i);

There are two common ways to use reserve to avoid unneeded reallocations. The first is applicable when you know exactly or approximately how many elements will ultimately end up in your container. The second way is to reserve the maximum space you could ever need, then, once you’ve added all your data, trim off any excess capacity.

C++之Effective STL学习笔记Item14的更多相关文章

  1. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  2. 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 ...

  3. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  4. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  5. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  6. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  7. 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 ...

  8. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  9. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

随机推荐

  1. vue props 传入对象Object,如果外层更改属性,默认里面是不更新,需要使用 this.$set(this.datese1, 'xsfaDateYear1', '')

    vue props 传入对象Object,如果外层更改属性,默认里面是不更新,需要使用 this.$set(this.datese1, 'xsfaDateYear1', '')

  2. vscode的eslint插件不起作用

    最近在用vue进行开发,但是vsCode中的eslint插件装上之后不起作用 1.vsCode打开“设置”,选择"settings.json" 2.输入一段脚本 "esl ...

  3. 浅谈JavaScript中的正则表达式(适用初学者观看)

    浅谈JavaScript中的正则表达式 1.什么是正则表达式(RegExp)? 官方定义: 正则表达式是一种特殊的字符串模式,用于匹配一组字符串,就好比用模具做产品,而正则就是这个模具,定义一种规则去 ...

  4. Tarjan算法 详解+心得

    Tarjan算法是由Robert Tarjan(罗伯特·塔扬,不知有几位大神读对过这个名字) 发明的求有向图中强连通分量的算法. 预备知识:有向图,强连通. 有向图:由有向边的构成的图.需要注意的是这 ...

  5. centos 安装 python3 分类链接

    上一篇文章描述了如何安装python3,但是在后续安装pip便不断报出缺少各类模块,安装一个又需要依赖另一个,导致安装过程非常繁琐.究其原因,我是安装centos-minimal版本,有许多功能不是完 ...

  6. 201621123080 《Java程序设计》第13周学习总结

    201621123080 <Java程序设计>第13周学习总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能( ...

  7. Python9-网络编程3-day32

    解决黏包的问题 #server import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() conn,addr ...

  8. STM32CUBEMX入门学习笔记2:关于STM32芯片使用内部flash

    找到正点原子的官网,下载他的HAL库:http://www.openedv.com/thread-109778-1-1.html 找到此例程,并打开其工程文件. 找到此文件,复制到自己工程里 复制到自 ...

  9. LeetCode(279)Perfect Squares

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  10. NXP低功耗蓝牙集成芯片QN9080C的时钟配置

    /*************************************************************************************************** ...