stl reserve

C ++ vector :: reserve()函数 (C++ vector::reserve() function)

vector::reserve() is a library function of "vector" header, which is used to request change in vector allocation. Refer to example to understand in details.

vector :: reserve()“ vector”头的库函数,用于请求矢量分配的更改。 请参阅示例以详细了解。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::reserve() function

vector :: reserve()函数的语法

    vector::reserve(n);

Parameter(s): int n – It accepts n as a parameter where n be the input capacity.

参数: int n –接受n作为参数,其中n是输入容量。

Return value: void – It returns nothing in case of valid request. But if the capacity requested is greater than the maximum size of the vector (vector::max_size), a length_error exception is thrown.

返回值: void –在有效请求的情况下不返回任何内容。 但是,如果请求的容量大于向量的最大大小( vector :: max_size ),则会引发length_error异常。

Example: Case 1: (without reserve())

示例:情况1 :(没有reserve())

vector<int> arr1; //usual dynamic allocation
size = arr1.capacity();
cout << "arr1 growing with usual dynamic allocation:\n";
for (int i = 0; i < 50; ++i) {
arr1.push_back(i);
if (size != arr1.capacity()) {
size = arr1.capacity();
cout << "capacity changed to : " << size << '\n';
}
}

In this case, we have not used reserve thus the growth is as per dynamic allocation, which increases in a factor of two. Like, 1, 2, 4, 8, 16, 32, 64, 128…..so on till max_size.

在这种情况下,我们没有使用储备金,因此增长是按动态分配进行的,增长了两倍。 像1,2,4,8,16,16,32,64,128…..so直到max_size

Example: Case 2: (with reserve())

示例:情况2 :(带有reserve())

vector<int> arr2; //using reserve
size = arr2.capacity();
arr2.reserve(50); // use of reserve function
cout << "arr2 growing with using reverse:\n";
for (int i = 0; i < 50; ++i) {
arr2.push_back(i);
if (size != arr2.capacity()) {
size = arr2.capacity();
cout << "capacity changed to: " << size << '\n';
}
}

In this case, we have not used reserve thus the growth is as per dynamic allocation, which increases in a factor of two. Like, 1, 2, 4, 8, 16, 32, 64, 128…..so on till max_size.

在这种情况下,我们没有使用储备金,因此增长是按动态分配进行的,增长了两倍。 像1,2,4,8,16,16,32,64,128…..so直到max_size

C ++程序演示vector :: reserve()函数的示例 (C++ program to demonstrate example of vector::reserve() function)

#include <iostream>
#include <vector>
using namespace std;
int main()
{vector<int>::size_type size;
vector<int> arr1; //usual dynamic allocation
size = arr1.capacity();
cout << "arr1 growing with usual dynamic allocation:\n";
for (int i = 0; i < 50; ++i) {arr1.push_back(i);
if (size != arr1.capacity()) {size = arr1.capacity();
cout << "capacity changed to : " << size << '\n';
}
}
vector<int> arr2; //using reserve
size = arr2.capacity();
arr2.reserve(50); // use of reserve function
cout << "arr2 growing with using reverse:\n";
for (int i = 0; i < 50; ++i) {arr2.push_back(i);
if (size != arr2.capacity()) {size = arr2.capacity();
cout << "capacity changed to: " << size << '\n';
}
}
return 0;
}

Output

输出量

arr1 growing with usual dynamic allocation:
capacity changed to : 1
capacity changed to : 2
capacity changed to : 4
capacity changed to : 8
capacity changed to : 16
capacity changed to : 32
capacity changed to : 64
arr2 growing with using reverse:
capacity changed to: 50

Reference: C++ vector::reserve()

参考: C ++ vector :: reserve()

翻译自: https://www.includehelp.com/stl/vector-reserve-function-with-example.aspx

stl reserve

stl reserve_vector :: reserve()函数以及C ++ STL中的示例相关推荐

  1. C++ STL的reserve函数

    在阅读ceph源码过程中发现部分C++语法还是不够熟悉,特此做一下笔记. 关于STL中的reserve函数的使用 reserve()是为容器预留空间,即为当前容器设定一个空间分配的阈值,但是并不会为容 ...

  2. c语言 函数的参数传递示例_restder()函数,带有C ++中的示例

    c语言 函数的参数传递示例 C ++ restder()函数 (C++ remainder() function) remainder() function is a library function ...

  3. c语言atoll函数怎么用_C ++中带有示例的atoll()函数

    c语言atoll函数怎么用 C ++ Atoll()函数 (C++ atoll() function) atoll() function is a library function of cstdli ...

  4. 18函数对象19command模式20函数对象在STL中的应用

    Item 18. Function Objects Item 19. Commands and Hollywood Item 20. STL Function Objects 1.unction Ob ...

  5. stl中copy()函数_std :: copy_if()函数以及C ++ STL中的示例

    stl中copy()函数 C ++ STL std :: copy_if()函数 (C++ STL std::copy_if() function) copy_if() function is a l ...

  6. c++stl和std_std :: replace()函数以及C ++ STL中的示例

    c++stl和std C ++ STL std :: replace()函数 (C++ STL std::replace() function) replace() function is a lib ...

  7. stl中copy()函数_std :: rotate_copy()函数以及C ++ STL中的示例

    stl中copy()函数 C ++ STL std :: rotate_copy()函数 (C++ STL std::rotate_copy() function) rotate_copy() fun ...

  8. stl min函数_std :: min()函数以及C ++ STL中的示例

    stl min函数 C ++ STL std :: min()函数 (C++ STL std::min() function) min() function is a library function ...

  9. stl中copy()函数_std :: copy()函数以及C ++ STL中的示例

    stl中copy()函数 C ++ STL std :: copy()函数 (C++ STL std::copy() function) copy() function is a library fu ...

最新文章

  1. 编写Java 制作留言板_JSP+Servlet+JavaBean制作简单留言板
  2. Google Drive客户端
  3. python3 一些常用的数学函数
  4. Python 标准库之单元测试框架 -- unittest
  5. js 获取浏览器高度和宽度值
  6. C# 数据库连接字符串拼接
  7. python通过SNMP协议收集服务器监控信息(安装、配置、示例)
  8. Hive中文件存储格式及大小比较测试
  9. 【渝粤教育】国家开放大学2018年春季 7406-21T金融统计分析 参考试题
  10. 关于Bitmapimage图片保存(png格式)
  11. 宏基aspire拆机触摸_(图) 宏基 Acer 4741G 完全拆解
  12. 海思Hi3519A开发(5.梳理海思文档与运行sample代码)
  13. Verilog无符号除法器-状态机实现
  14. iOS14捷径------番茄钟2.0
  15. 智齿客服H5聊天链接接入及WebView不支持input file文件上传解决
  16. 北京-IT技术狗-顾名思义 解释一下当时随手写下这个名字
  17. 采购订单税码检查增强(badi)
  18. VLC控件支持的参数和方法
  19. SQL xin手错误鉴赏以及成长小结
  20. JMM 8 大原子操作

热门文章

  1. 项目一计算机基础知识考核题,2013计算机基础知识试题及答案
  2. java treetable_在Swing中创建TreeTable | 学步园
  3. 主动断开socket链接_TCP连接与断开详解(socket通信)
  4. opencv获取模板旋转角度_OpenCV入门之获取图像的旋转角度
  5. Python List:合并多个list,listd的合并
  6. Java并发篇_乐观锁与悲观锁
  7. Angular深入理解之指令
  8. 蓝牙基础知识进阶——Physical channel
  9. Jquery简单的右侧浮动菜单
  10. HDU 6188 Duizi and Shunzi