使用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. [web笔记]解决跨域问题以及axios每次提交session变化的问题

  2. 重新postgresql出现错误:Problem running post-install step. Installation may not complete correctly. The database cluster initialisation failed.

    以前正常使用的postgresql,今天出现问题:报*.dll错误.百度了一下,只能重新安装 . 在重新安装过程中报:Problem running post-install step. Instal ...

  3. springboot 内置tomcat maxPostSizs 无法设置

    ++++++++++++++++++ RT +++++++++++++++++ 如下代码方可解决: /** * tomcat配置类 * 解决post数据体大于2048kb无法接收问题 * 解决tomc ...

  4. java中插入myslq的datetime类型的

    java.util.Date d = new java.util.Date(); Timestamp tt=new Timestamp(d.getTime()); ps.setTimestamp(4, ...

  5. SSH框架使用poi插件实现Excel的导入导出功能

    采用POI生成excel结构 直接贴出代码  excel表格导出功能 action代码: struts.xml配置: 前台jsp代码:

  6. TCP/IP协议头部结构体

    TCP/IP协议头部结构体(转) 网络协议结构体定义 // i386 is little_endian. #ifndef LITTLE_ENDIAN #define LITTLE_ENDIAN (1) ...

  7. cesium 基于天地图服务 完成底图标注渲染加切换

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. PHP使用FTP上传文件到服务器(实战篇)

    我们在做开发的过程中,上传文件肯定是避免不了的,平常我们的程序和上传的文件都在一个服务器上,我们也可以使用第三方sdk上传文件,但是文件在第三方服务器上.现在我们使用PHP的ftp功能把文件上传到我们 ...

  9. C++简易酒店管理系统,实现(查询、入住、退房、楼层选择、退出)功能

    #include <iostream> #include <string.h> #include <stdlib.h> void enter(); void che ...

  10. PHP将unicode转utf8最简法

    最近开发时遇到Unicode编码问题,找了半天才知道PHP并没有Unicode转码函数,终于发现用一行PHP代码解决的方案: $str = '{"success":true,&qu ...