C++之Effective STL学习笔记Item14
使用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的更多相关文章
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 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 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 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 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
随机推荐
- Java 文件操作-RandomAccessFile
1. RandomAccessFile Java提供了一个可以对文件随机访问的操作,访问包括读和写操作.该类名为RandomAccessFile.该类的读写是基于指针的操作. 1)文件访问模式 ...
- 基于FPGA的DDS任意波形发生器设计
一.简介 DDS技术最初是作为频率合成技术提出的,由于其易于控制,相位连续,输出频率稳定度高,分辨率高, 频率转换速度快等优点,现在被广泛应用于任意波形发生器(AWG).基于DDS技术的任 ...
- TC和脚本语言
TC:Turbo C 集成开发环境是由Borland 公司开发的一套C 语言开发工具,它集成了程序编辑.调试.链接等多种功能.在DOS 系统时代,Turbo C 是被最广泛使用的一种PC 机应用程序开 ...
- JPA + EclipseLink + SAP云平台 = 运行在云端的数据库应用
JPA(Java Persistence API)的实现Provider有Hibernate,OpenJPA和EclipseLink等等. 本文介绍如何通过JPA + Eclipse连接SAP云平台上 ...
- unity热更新方案对比
Unity应用的iOS热更新 • 什么是热更新 • 为何要热更新 • 怎样在iOS 上对Unity 应用进行热更新 • 支持Unity iOS 热更新的各种Lua 插件的对照 什么是热更新 • ...
- 解决nginx bind() to 0.0.0.0:80 failed 问题
nginx的配置文件一开始默认是80端口,出现这个错误多半是80端口已经被占用.这时候只需要把 server { listen 8088; server_name localhost lcsf.com ...
- php循环a-z字母表
ord — 返回字符的 ASCII 码值 说明 int ord ( string $string ) 返回字符串 string 第一个字符的 ASCII 码值. 该函数是 chr() 的互补函数. ...
- 谷歌浏览器 加安全地址 快捷方式加参数 chrome
--unsafely-treat-insecure-origin-as-secure="http://192.168.43.17:8080"
- 2018.2.10 使用SSH连接远程滴滴云服务器Ubuntu (Windows下) 及 putty工具永久设置字体、颜色
一开始会有人问云服务器是什么? 云服务器是一种类似VPS服务器的虚拟化技术, VPS是采用虚拟软件,VZ或VM在一台服务器上虚拟出多个类似独立服务器的部分,每个部分都可以做单独的操作系统,管理方法同服 ...
- Vue-Quill-Editor 修改配置,和图片上传
1.富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改配置. 创建一个quill-config文件 /*富文本编辑图片上传配置*/ /*富文本编辑图片上传 ...