vector的capacity增长方式

vector的capacity()调用返回vector中最大能够存储的元素个数,也即在下一次需要扩充容量之前能容纳的元素个数。reserve会使容器在必要的时候增长,以便容纳制指定数目的元素。

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> u;
    cout << "Initial capaciry of u: " << u.capacity() << "\n";
    for(int i = 0; i < 40; ++i)
    {
        u.push_back(i);
        cout<<"size is "<<u.size()<<" ";
        cout<<"Now capacity is :" << u.capacity() << "\n";
    }
    return 0;
}

运行环境:win7 32bit codeblocks

Initial capaciry of u: 0
size is 1 Now capacity is :1
size is 2 Now capacity is :2
size is 3 Now capacity is :4
size is 4 Now capacity is :4
size is 5 Now capacity is :8
size is 6 Now capacity is :8
size is 7 Now capacity is :8
size is 8 Now capacity is :8
size is 9 Now capacity is :16
size is 10 Now capacity is :16
size is 11 Now capacity is :16
size is 12 Now capacity is :16
size is 13 Now capacity is :16
size is 14 Now capacity is :16
size is 15 Now capacity is :16
size is 16 Now capacity is :16
size is 17 Now capacity is :32
size is 18 Now capacity is :32
size is 19 Now capacity is :32
size is 20 Now capacity is :32
size is 21 Now capacity is :32
size is 22 Now capacity is :32
size is 23 Now capacity is :32
size is 24 Now capacity is :32
size is 25 Now capacity is :32
size is 26 Now capacity is :32
size is 27 Now capacity is :32
size is 28 Now capacity is :32
size is 29 Now capacity is :32
size is 30 Now capacity is :32
size is 31 Now capacity is :32
size is 32 Now capacity is :32
size is 33 Now capacity is :64
size is 34 Now capacity is :64
size is 35 Now capacity is :64
size is 36 Now capacity is :64
size is 37 Now capacity is :64
size is 38 Now capacity is :64
size is 39 Now capacity is :64
size is 40 Now capacity is :64

vector的capacity增长方式,布布扣,bubuko.com

时间: 2024-10-06 04:13:34

vector的capacity增长方式的相关文章

stackoverflow: Why the inital capacity of vector is increasing with the formula 2^N? 为什么vector的capacity是按照2的指数来长的?(原文+翻译)

原文:https://stackoverflow.com/questions/5232198/about-vectors-growth 翻译:joey Answer 1: The rate at which the capacity of a vector grows is implementation dependent. Implementations almost invariably choose exponential growth, in order to meet the amor

C++学习之路: STL探索 vector 的自增长

这篇我们讨论一下vector 自增长的体现 1 #include <iostream> 2 #include <string> 3 #include <vector> 4 using namespace std; 5 6 int main(int argc, const char *argv[]) 7 { 8 vector<int> vec; 9 vec.push_back(12); 10 11 cout << vec.size() <&l

C++ std::vector 三种遍历方式的效率比较

#include <iostream> #include <vector> #include <stdint.h> #include <ctime> int main() { const uint32_t loop = 1000000; std::vector<int32_t> vec; clock_t timeStart = 0; for (uint32_t i = 0; i < loop; ++i) { vec.push_back(i)

Vector大小的增长和线程安全

int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ?                                  capacityIncrement : oldCapacity); 从以上可以看出,Vectory中如果大小超出范围,则增长一倍扩大. public synchronized boolean add(E e) {     modCount+

面试 C++ 程序员,什么样的问题是好问题?

原文链接: <陈硕大神在知乎的回答> 对于应届生: 标准库各容器的基本操作的复杂度.标准库算法的复杂度,例如 std::sort() 的平均复杂度.最坏复杂度(答 O(N^2) 和 O(N log N) 都算对),最坏情况什么时候出现. 标准库各容器(deque 除外)的数据结构(标准党勿喷,主流 STL 实现的数据结构都差不多),以及 vector 的容量增长方式.如果回答得特别好,还可以补充问为什么 vector::push_back() 的复杂度是分摊之后的 O(1),作为加分. 出一道

c++ vector(向量)使用方法详解(顺序访问vector的多种方式)

来源:http://www.jb51.net/article/44231.htm 作者: 字体:[增加 减小] 类型:转载 时间:2013-12-08我要评论 vector是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器,本文介绍一下使用方法 vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器.vector 是C++ STL的一个重要成员,使用它时需要包含头文件: 复制代码 代码如下: #include<vector>; 一.vector 的初始

vector的reserve和resize以及capacity和size

1.capacity 指容器在不分配新的存储空间的前提下它最多可以保存多少元素. 2. size 指当前容器已经保存的元素的数目. 在弄清这两个概念以后,很容易懂resize和reserve的区别 vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector的capacity同时也增加了它的size! 原因如下: reserve是容器预留空间,但在空间内不真正创建元素对象,所以在没有添加新的对象之前,不能引用容器内的元素.加入新的

STL之vector详解

一.vector容器的自增长 首先,我们知道vector容器是由数组做出来的:它具备了数组的优缺点. 数组的优点: 操作数据,读取速度很快,因为有下标: 数组的缺点: 分配之后不能在改变大小: 1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 int main() 7 { 8 int bb[3]; 9 bb[0] = 1; 10 bb[1] = 2; 11 bb[2] = 3; 12 13

ArrayList、LinkedList、 Vector、Map 用法比较

ArrayList和Vector是采用数组方式存储数据,此数组元素总数大于实际存储的数据个数以便增加和插入元素,二者都允许直接序号索引元素,但是插入数据要移动数组元素等内存操作,所以它们索引数据快.插入数据慢. ArrayList数组存储方式: [java] view plaincopyprint? private transient Object[] elementData; public ArrayList(int initialCapacity) { super(); if (initia