前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >std::vector扩容机制,.按1.5倍取整扩容,如下。

std::vector扩容机制,.按1.5倍取整扩容,如下。

作者头像
forxtz
发布2020-10-10 16:40:54
6870
发布2020-10-10 16:40:54
举报
文章被收录于专栏:源懒由码
代码语言:javascript
复制
#include <iostream>
#include <vector>

using namespace std;

/*
 * 结论:
 * capacity = 1, newcapacity = 2;
 * capacity > 1, newcapacity = (int)(capacity * 1.5);
 */
int main(){
    int cap = -1;
    vector<int> ints;
    for(int i = 0;i<1000000;i++){
        ints.push_back(i);
        if( cap != ints.capacity()){
            cap = ints.capacity();
            cout << " ints.size() = " << ints.size() << "  ints.capacity() = " << cap << endl;
        }
    }
    cout << (int)(3*1.5) << endl;
    system("pause");
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档