zoukankan      html  css  js  c++  java
  • [C++]vector内存的增长机制

    例子

    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include "CPPDemo.h"
    #include<iomanip>
    #include<set>
    #include<vector>
    using namespace std;
    
    int main() {
    	vector<int> num;
    	int i = 1;
    	unsigned int capacity = num.capacity();
    
    	for (int temp = 0; temp < 1000; temp++)
    	{
    		num.push_back(1);
    		//容量发生改变时输出
    		if (num.capacity() != capacity)
    		{
    			capacity = num.capacity();
    			cout << num.capacity() << endl;
    		}
    
    	}
    
    
    	return 0;
    }
    
    

     输出结果:

    可以看出,每次增长的时候都是原来的容量×1.5倍=新容量

    IDE:VS2017

  • 相关阅读:
    10.18
    10.16~10.17笔记
    JS
    10.8~10.11
    9.28~9.29
    9.27 代码笔记
    代码复习(9.26)
    9.19 链家
    9.18笔记
    9.17 定位
  • 原文地址:https://www.cnblogs.com/lizhenghao126/p/11053555.html
Copyright © 2011-2022 走看看