使用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. BZOJ 4175: 小G的电话本 SAM+FFT

    4175: 小G的电话本 Time Limit: 45 Sec  Memory Limit: 256 MBSubmit: 195  Solved: 48[Submit][Status][Discuss ...

  2. Oracle RAC Brain Split Resolution

    大约是一周前,一位资深的Oracle工程师向我和客户介绍RAC中脑裂的处理过程,据他介绍脑裂发生时通过各节点对voting disk(投票磁盘)的抢夺,那些争抢到(n/2+1)数量voting dis ...

  3. Android(java)学习笔记132:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。

    导致这个错误的原因是工程重名了: 并不是仅仅指文件夹重名,相信很多人也曾经修改过文件夹的名称,可惜没什么用处,关键是修改工程里面的一个文件! 也就是.project这个文件! 用记事本打开,修改一下& ...

  4. Problem A: 文件操作--二进制文件读入

    Problem A: 文件操作--二进制文件读入 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1952  Solved: 524[Submit][St ...

  5. 2018.4.15 Mac系统下如何使用StartUml画好需求分析的类图 (同样适用于windows)

    Mac如何使用StartUml (同样适用于windows) 左侧边栏的英文含义及其用法 关联(Association) [关联关系]:是一种拥有的关系,它使一个类知道另一个类的属性和方法:如:老师与 ...

  6. 课外作业1:将一个double类型的小数,按照四舍五入保留两位小数

    package come.one01; public class One02 { public static void main(String[] args) { double numa = 3.14 ...

  7. Bootstrap 页面标题(Page Header)

    Bootstrap页面标题(PageHeader)是个不错功能,它会网页的标题的四周添加适当的间距,当一个网页中有多个标题并且每个标题之间需要添加一定适当的间距,使用页面标题是非常有用的.如果需要使用 ...

  8. ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)

    在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...

  9. PHP计算两个日期相差的年月日时分秒

    $start_time = '2017-09-06 15:12:20'; $end_time = '2018-09-08 10:20:45'; get_time($start_time,$end_ti ...

  10. NodeJS基础API-path相关的问题basename,extname,dirname,parse,format,sep,delimiter,win32,posix

    path 参考文档:http://nodejs.cn/api/path.html const {normalize} = require('path'); // ES6语法 // 相当于 const ...